I am kind of new to unity and I have a problem with GetComponent…
I want a door to open when my Player hits a cube tagged “SwitchTag” with a boolean:
(Main Script:)
[...]
OnCollisionEnter (Collision collision){
if (collision.gameObject.tag == "SwitchTag")
{
DoorTutorial.GetComponent<DoorSwitch>().SwitchOn = true;
sfx_door.Play();
}
}
(Door Script:)
public class DoorSwitch : MonoBehaviour {
public bool SwitchOn;
private Vector3 startPos;
// Use this for initialization
void Start () {
startPos = transform.position;
SwitchOn = false;
}
// Update is called once per frame
void Update ()
{
if(SwitchOn)
{
transform.Translate (0, 0, 3 * Time.deltaTime);
if (transform.position.z > startPos.z + 1)
SwitchOn = false;
}
}
}
But it doesn’t work …
Any help would be appreciated ![]()
How have you declared and initialized 'DoorTutorial' in the first script? When you say 'doesn't work' are you having a compiletime error, runtime error, or it just doesn't do anything?
– robertbuI had this: NullReferenceException: Object reference not set to an instance of an object And the door did not move
– SC4V4NGERTo make the hole thing a bit clearer: I have tried to activate the boolen wit a Key instead of a collison, just to look if it works: if(Input.GetKeyDown(KeyCode.Q)) { //if (hit.gameObject.CompareTag("SwitchTag")){ TuerTutorial.GetComponent<TuerSwitch>().auf = true; sfx_grenze.Play(); } But it still doesn't work
– SC4V4NGER