我試圖讓玩家按下按鈕時傳送帶扭轉方向。用GetComponent C編輯變量#
下面是傳送帶
using UnityEngine;
using System.Collections;
public class Conveyor : MonoBehaviour {
public float speed = 1.0f;
void OnTriggerStay(Collider col)
{
col.transform.position += transform.forward * speed * Time.deltaTime;
}
}
的代碼和按鈕的代碼
public class PushButton : MonoBehaviour
{
public GameObject Button;
private Conveyor conveyor;
void Awake()
{
conveyor = GetComponent<Conveyor>();
}
void OnTriggerStay(Collider entity)
{
if (entity.tag == "Player")
{
if (Input.GetKeyUp(KeyCode.E))
{
conveyor.speed = conveyor.speed * -1;
}
}
}
}
我得到一個錯誤說「對象未設置爲一個對象按鈕的實例.OnTriggerStay(Unity Engine.Collider實體)(在Assests/PushButton.cs21)
我還不是很熟悉使用getComponent,所以我不知道如何解決這個問題。不勝感激。
注意「public GameObject Button」變量必須是小寫字母,所以它會是「按鈕」。另外請注意,Unity中有一個叫做「Button」的東西,所以你可能不會使用它。例如,將其稱爲「topLeftRedButton」 – Fattie