2016-12-11 73 views
1

我正在嘗試製作賽車遊戲。在這場比賽中我已經做了兩個按鈕和附加的腳本carControllerEventTrigger上的指針列如下: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(); 
        } 



     } 


    } 

現在我已經嘗試了按鍵觸摸和滑動觸摸,但都沒有工作!

這是給這個錯誤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() 

和代碼行是這樣的:

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如下:

carspeed=4在編輯器它是公開的

+0

你得到一個NullReferenceException,所以請發佈錯誤日誌 – Opiatefuchs

+0

我已經發布了 – utkdub

+0

上面的圖片@Opiatefuchs – utkdub

回答

0

rb = GetComponent<Rigidbody2D>();失敗,因爲沒有Rigidbody2D附加到腳本附加到的GameObject。

請附上Rigidbody2D該腳本附加到的GameObject carController

您還可以

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

更換

rb = GetComponent<Rigidbody2D>();

,如果你不想在carController腳本附加到遊戲物體與Rigidbody2D

+0

Rigidbody2D附加爲腳本附加到'汽車'遊戲對象 – utkdub

+0

請參閱上面鏈接的第一張圖片@Programmer – utkdub

+0

看看並確保沒有'carController'腳本附加到另一個GameObject。 – Programmer