2016-05-02 22 views
-1

NullReferenceException:オブジェクトリファレンスがオブジェクトのインスタンスに設定されていませんTouchHandler.TouchControl()(Assets/Script/TouchHandler.cs:78)TouchHandler.Update()(資産/スクリプト/ TouchHandler.csで:39)Unity3D:NullReferenceExceptionの解決策を見つけることができません

ray = Camera.main.ScreenPointToRay(touch.position); LINE 78

私はこのエラーを取得し、私はその理由を把握傾けます。新鮮な目が助けになると思った、誰?
ありがとうございます。

using UnityEngine; 
using System.Collections; 

public class TouchHandler : MonoBehaviour { 


    /*****All the touch variables******/ 
    private Vector2  fp;    // first finger position 
    private Vector2  lp;    // last finger position 
    private float  angle;   
    private float  swipeDistanceX; 
    private float  swipeDistanceY; 
    private int   swipeDistance = 50; // Distance fingure to travell to register as a swipe 
    private Touch  touch;   // touch variable  



    /*****All the Raycast variables******/ 
    Ray ray; 
    RaycastHit hitInfo = new RaycastHit(); 

    /*****All the PlayerController script variables******/ 
    private PlayerController PC_component; 


    //private MovementHandler movementHandlerScriptComponent; 


    void Start() 
    { 
     PC_component = GetComponent<PlayerController>(); 

     //movementHandlerScriptComponent = GetComponent<MovementHandler>(); 
     //movementHandlerScriptComponent.SetisBaseNameSet(false); 
    } 

    void Update() 
    { 
     TouchControl(); 
    } 

    void OnGUI() 
    { 
     foreach(Touch t in Input.touches) 
     { 
      string message = ""; 

      message += "ID: "  + t.fingerId   + "\n"; 
      message += "Phase: " + t.phase.ToString() + "\n"; 
      message += "TapCount: " + t.tapCount   + "\n"; 
      message += "X: "  + t.position.x   + "\n"; 
      message += "Y: "  + t.position.y   + "\n"; 
      message += "Delta: " + t.deltaPosition  + "\n"; 
      int num = t.fingerId; 

      GUI.Label(new Rect(0 + 130 * num, 0, 120, 120), message); 

     } 
    } 
    void castingRay() 
    { 
     if(Physics.Raycast(ray,out hitInfo)) 
     { 
      if(hitInfo.transform.tag == "Base") 
      { 
       PC_component.spawnPlayer(hitInfo); 

       //movementHandlerScriptComponent.Setsb_(hitInfo); 
       //movementHandlerScriptComponent.SetisBaseNameSet(true); 
      } 
     } 
    } 
    void TouchControl() 
    { 
     if(Input.touchCount == 1) 
     { 
      touch = Input.GetTouch(0); 
      ray = Camera.main.ScreenPointToRay(touch.position); 

      if (touch.phase == TouchPhase.Began) 
      { 
       fp = touch.position; 
       lp = touch.position; 

       //Raycasting 
       //if(movementHandlerScriptComponent.GetisBaseNameSet() == false) 
       if(PC_component.isPlayerSpawned == false) 
        castingRay(); 
      } 
      if (touch.phase == TouchPhase.Moved) 
      { 
       lp = touch.position; 
       swipeDistanceX = Mathf.Abs((lp.x-fp.x)); 
       swipeDistanceY = Mathf.Abs((lp.y-fp.y)); 
      } 
      if(touch.phase == TouchPhase.Ended) 
      { 
       angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f; 
       swipeControlls(); 
      } 
     } 
    } 

    void swipeControlls() 
    { 
     if(angle > 60 && angle < 120 && swipeDistanceX > swipeDistance) 
     { 
      Debug.Log("right"); 
      PC_component.moveDirection = "right"; 
     } 
     if(angle > 150 || angle < -150 && swipeDistanceY > swipeDistance) 
     { 
      Debug.Log("down"); 
     } 
     if(angle < -60 && angle > -120 && swipeDistanceX > swipeDistance) 
     { 
      Debug.Log("left"); 
      PC_component.moveDirection = "left"; 
     } 
     if(angle > -30 && angle < 30 && swipeDistanceY > swipeDistance) 
     { 
      Debug.Log("up"); 
     } 
    } 



} 
+1

これは78行目ですか? –

+0

@JonSkeet Sorry ray = Camera.main.ScreenPointToRay(touch.position);この1つは –

+1

です。「touch」がnullでないことを確認しましたか?そして、「Camera.main」?それらは明らかな可能性があります。 –

答えて

2

カメラに「MainCamera」タグが割り当てられていることを確認してください。

他のタグが割り当てられている場合、メインカメラとはみなされません。

+0

仲間は魅力的に働いてくれてありがとう! –

関連する問題