0

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; 
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Looks to me like "red" or "red.GetComponent() is returning a null value. So maybe when you instantiate red using "red = gameobject.find("red"), this is returning null, or the GetComponent() method is returning null inside OnTriggerEnter – Shane Sepac Jan 08 '17 at 18:11
  • Obviously you do not have "Red" gameobject or it doesnt have "PingPong" component – Łukasz Motyczka Jan 08 '17 at 18:11
  • red.GetComponent() is returning null, i have the script attached but it doesn't work :S –  Jan 08 '17 at 18:13
  • i did debug.log just on my red object and it returns a gameobject :/ –  Jan 08 '17 at 18:13
  • Please troubleshoot your own code with an if statement and Debug.Log(). You asked 3 questions in a row about null reference error and that's bad! Check the edit I made in your question. Good luck! – Programmer Jan 08 '17 at 18:20

0 Answers0