私は車レーシングゲームを作ろうとしています。Androidタッチ入力NullReferenceException
車のコントローラスクリプトは、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れます公開です
あなたはNullReferenceExceptionを取得しますので、エラーログ – Opiatefuchs
を投稿してください。 – utkdub
を@Opiatefuchsの上の画像 – utkdub