i can't figure out why i have a nullreference exception on my code, basicly i have 9 objects all of them have a script that i want activate via other script onTriggerEnter
So i did this: (i know i can use a foor lop but now i want just test it)
public class DoorEnter : MonoBehaviour {
private GameObject red;
private GameObject red1;
private GameObject red2;
private GameObject red3;
private GameObject red4;
private GameObject red5;
private GameObject red6;
private GameObject red7;
private GameObject red8;
private GameObject red9;
// Use this for initialization
void Start () {
red = GameObject.Find ("Red");
red1 = GameObject.Find ("Red1");
red2 = GameObject.Find ("Red2");
red3 = GameObject.Find ("Red3");
red4 = GameObject.Find ("Red4");
red5 = GameObject.Find ("Red5");
red6 = GameObject.Find ("Red6");
red7 = GameObject.Find ("Red7");
red8 = GameObject.Find ("Red8");
red9 = GameObject.Find ("Red9");
}
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player") {
Debug.Log (red);
red.GetComponent<PingPong> ().enabled = true; // this line
red1.GetComponent<PingPong> ().enabled = true;
red2.GetComponent<PingPong> ().enabled = true;
red3.GetComponent<PingPong> ().enabled = true;
red4.GetComponent<pingPongX> ().enabled = true;
red5.GetComponent<pingPongX> ().enabled = true;
red6.GetComponent<pingPongX> ().enabled = true;
red7.GetComponent<PingPong> ().enabled = true;
red8.GetComponent<pingPongX> ().enabled = true;
red9.GetComponent<pingPongX> ().enabled = true;
}
}
}
i get a null reference exception "this line" as you guys can see above, what am i doing wrong? i just want to access other gameobject scripts and enable them when the trigger is activated :S
EDIT:
Things I could have done before posting:
Make sure that red
Gameobject is not null
before doingnGetComponent
on it.
If red
GameObject is null then make sure that it is not disabled before red = GameObject.Find ("Red");
is called.
Make sure that GetComponent<PingPong>()
is not null
before calling its enabled
property.
If GetComponent<PingPong>()
is null then attach PingPong
script to the red
Gameobject. That's it. It simply means that the PingPong script is not attached to the red
GameObject.
if(red != null)
{
PingPong pg = red.GetComponent<PingPong>();
if(pg != null)
{
pg.enabled = true;
}
}