Hello Peeps, I’m no good at scripting so I could really do with some help.
I have 2 scripts that I am using, one works fine and the other doesn’t even though, as far as I’m aware, they are performing the same function.
The first script is a component of a box trigger that deactivates two other scripts that are on my player character and control his movement when the player enters that trigger. The player is then activated again on a button press:
public KeyCode Deactivate;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (Deactivate)) {
Cursor.lockState = CursorLockMode.Locked;
gameObject.SetActive (false);
GameObject.Find ("Player").GetComponent<CharacterMover> ().enabled = true;
GameObject.Find ("Player").GetComponent<CharacterTurner> ().enabled = true;
}
}
void OnTriggerEnter(Collider other) {
Cursor.lockState = CursorLockMode.Confined;
if (other.CompareTag ("Player")) {
GameObject.Find("Player").GetComponent<CharacterMover>().enabled = false;
GameObject.Find("Player").GetComponent<CharacterTurner>().enabled = false;
}
}
}
That works fine, however another script that I want to activate a deactivated button on the same trigger scenario…:
void OnTriggerEnter(Collider other) {
if (other.CompareTag ("Player")) {
GameObject.Find ("Test Button 1").gameObject.SetActive (true);
}
}
… gives me the following error:
NullReferenceException: Object reference not set to an instance of an object
ButtonActivation.OnTriggerEnter (UnityEngine.Collider other) (at Assets/ButtonActivation.cs:17)
I know line 17 is ‘GameObject.Find (“Test Button 1”).gameObject.SetActive (true);’ but I don’t understand why i get an error, any help would be very much appreciated thanks!