0

i have two scripts, and i want them to acces one another, to set bools to true and false respectively. But i'm getting this error: NullReferenceException: Object reference not set to an instance of an object

Here's the first script:

using UnityEngine;
using System.Collections;

public class Colliders_overkant : MonoBehaviour 
{

    public bool overkantActive;

    GameObject personage;

    GameObject eerstekant;

    void Start () {
        overkantActive = true;
    }

    void Update () {
    }

    void OnTriggerEnter  ( Collider collider )
    {

        if (collider.tag == "Speler") 
        {
            if (overkantActive == true)
            {
                Debug.Log ("Overkant");

                eerstekant = GameObject.FindWithTag ("Eerstekant");
                personage = GameObject.FindWithTag ("Speler");
                personage.transform.Rotate (0 , 180, 0);
                gameObject.GetComponent<Colliders_eerstekant>().eerstekantActive = true;
                overkantActive = false;

            }
        }
    }
}

And here's the other:

using UnityEngine;
using System.Collections;

public class Colliders_eerstekant : MonoBehaviour 
{

    public bool eerstekantActive;

    GameObject personage;

    GameObject overkant;

    void Start () {
        eerstekantActive = false;
    }

    void Update () {
    }

    void OnTriggerEnter  ( Collider collider )
    {

        if (collider.tag == "Speler") 
        {
            if (eerstekantActive == true)
            {
                Debug.Log ("Eerstekant");

                personage = GameObject.FindWithTag ("Speler");
                overkant = GameObject.FindWithTag ("Overkant");
                personage.transform.Rotate (0 , 180, 0);
                gameObject.GetComponent<Colliders_overkant>().overkantActive = true;
                eerstekantActive = false;

            }
        }
    }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
GAMIE64
  • 5
  • 2
  • 5
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Jan 29 '15 at 00:08

1 Answers1

0

It's as it says on the tin, you're trying to access attributes and methods of variables set to null. The following statements that you have used will all return null if they fail to find what they are instructed to look for:

GameObject.FindWithTag ("Speler");
gameObject.GetComponent<Colliders_overkant>();

The first one means that there is no gameObject in the unity scene with the tag "Speler" if it returns null, and the second one means that there is no component on the gameObject called "Colliders_overkant" if it returns null.

Benjamin James Drury
  • 2,353
  • 1
  • 14
  • 27
  • It's about the last one, gameObject.GetComponent(); It's fine when i start, but the moment i go into the collider, and my character should turns, the bool overkantActive doesn't become true, since it can't be found. But it IS attached to a game object in the scene. – GAMIE64 Jan 29 '15 at 00:20