2016-12-11 12 views
1

私は車レーシングゲームを作ろうとしています。Androidタッチ入力NullReferenceException

CAR gameobject

車のコントローラスクリプトは、C#であり、次のとおりです:

public class carController : MonoBehaviour { 
     public float carSpeed; 
     Vector3 position; 
     public uiManager ui; 
     public audioManager am; 
     Rigidbody2D rb; 
     bool currentPlatformAndroid=false; 
     // Use this for initialization 

     void awake(){ 

      rb = GetComponent<Rigidbody2D>(); 
      #if UNITY_ANDROID 
      currentPlatformAndroid=true; 
      #else 
      currentPlatformAndroid=false; 
      #endif 
      am.carSound.Play(); 

     } 


     void Start() { 
      //ui = GetComponent<uiManager>(); no need 

      if (currentPlatformAndroid == true) { 

       Debug.Log ("Android"); 
      } else { 
       Debug.Log ("windows"); 
      } 

      position = transform.position; 
     } 

     // Update is called once per frame 
     void Update() { 
      if (currentPlatformAndroid == true) { 
       //android specific 
       TouchMove(); 

      } else { 
       position.x+=Input.GetAxis ("Horizontal") * carSpeed * Time.deltaTime; 
       position.x= Mathf.Clamp (position.x, -2.1f, 2.1f); 
       transform.position = position; 
      } 
      position = transform.position; 
      position.x= Mathf.Clamp (position.x, -2.1f, 2.1f); 
      transform.position = position; 



     } 

     void OnCollisionEnter2D(Collision2D col){ 

      if (col.gameObject.tag == "Enemy Car") { 
       gameObject.SetActive (false); 
       ui.gameOverF(); 
       am.carSound.Stop(); 
      } 

     } 

     public void MoveLeft(){ 
      rb.velocity = new Vector2 (-carSpeed, 0); 
     } 

     public void MoveRight(){ 
      rb.velocity = new Vector2 (carSpeed, 0); 
     } 

     public void SetVelocityZero(){ 

      rb.velocity = Vector2.zero; 
     } 

     void TouchMove(){ 
      if (Input.touchCount > 0) { 
       Touch touch = Input.GetTouch (0); 
       float middle=Screen.width/2; 
       if (touch.position.x < middle && touch.phase == TouchPhase.Began) { 
        MoveLeft(); 
       } 
       else if((touch.position.x > middle && touch.phase == TouchPhase.Began)){ 
        MoveRight(); 
       } 



        }else{ 
         SetVelocityZero(); 
        } 



     } 


    } 

今、次のようにこのゲームでは、私は、ポインタダウンへのEventTriggerに2つのボタンと付属のスクリプトcarControllerを行っています私はボタンタッチとスワイプタッチの両方を試みましたが、どちらも動作していません!

それは

enter image description hereが、私は理由を理解することはできませんよアムこのエラーを与えていますか?

誰でも手伝いできますか?次のように

主エラーログがある:

NullReferenceException: Object reference not set to an instance of an object 
carController.SetVelocityZero() (at Assets/scripts/carController.cs:78) 
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) 
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) 
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769) 
UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53) 
UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:67) 
UnityEngine.EventSystems.EventTrigger.OnPointerUp (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:98) 
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerUpHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:45) 
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerUpHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) 
UnityEngine.EventSystems.EventSystem:Update() 

、コードの行はこれです:エディタで

carspeed=4などを:

public void MoveLeft(){ 
     rb.velocity = new Vector2 (-carSpeed, 0); 
    } 

    public void MoveRight(){ 
     rb.velocity = new Vector2 (carSpeed, 0); 
    } 

    public void SetVelocityZero(){ 

     rb.velocity = Vector2.zero; 
    } 

次のようにCarspeedもinitalisedれます公開です

+0

あなたはNullReferenceExceptionを取得しますので、エラーログ – Opiatefuchs

+0

を投稿してください。 – utkdub

+0

を@Opiatefuchsの上の画像 – utkdub

答えて

0

rb = GetComponent<Rigidbody2D>();は、スクリプトが添付されたGameObjectに添付されているRigidbody2Dが存在しないため、失敗しています。

Rigidbody2Dには、carControllerスクリプトが添付されているゲームオブジェクトを添付してください。

あなたはRigidbody2DとそのゲームオブジェクトにcarControllerスクリプトを添付したくない場合にも、

GameObject.Find("NameOfGameObjectRigidBody2DIsAttachedTo").GetComponent<Rigidbody2D>();

rb = GetComponent<Rigidbody2D>();

を置き換えることができます。

+0

スクリプトが 'car'ゲームオブジェクトに添付されているので、Rigidbody2Dが添付されています – utkdub

+0

最初の画像を参照してください@ Programmer – utkdub

+0

他のGameObjectに' carController'スクリプトが添付されていないことを確認してください。 – Programmer

関連する問題