2
votes

So I've got two scripts, one is called EndCollider.cs it's somewhere on the map, It have one OnTriggerEnter function which set a boolean to true.

using UnityEngine;
using System.Collections;

public class EndCollider : MonoBehaviour {

public bool isShow = false;

void OnTriggerEnter ()
{
    isShow = true;
}

}

the other script is SlowDownRun.cs, it's on a monster object, in this script I'm trying to detect if the boolean from another script is true or not, if it is then move the monster.

using UnityEngine;
using System.Collections;

public class SlowDownRun : MonoBehaviour {

GameObject TrollScript;
EndCollider MonsterShow;

// Use this for initialization
void Start ()
{
    TrollScript = GameObject.Find("Troll");
    MonsterShow = TrollScript.GetComponent<EndCollider>();
}

void Update()
{
    if (MonsterShow.isShow == true)
    {
        float movementSpeed = 10f;
        transform.position += transform.forward * Time.deltaTime * movementSpeed;
    }
}

}

Now the code doesn't work, the code where move the monster works if I take them out the IF statement. I also keep getting this error NullReferenceException: Object reference not set to an instance of an object on the line if (MonsterShow.isShow == true)

Please help I'm a beginner to Unity and this problem stuck on me for so many hours now, I've done a lot Google searches and modified my code again and again but just can't solve this problem, I feel anxious and just don't know what to do.

3

3 Answers

0
votes

Your find troll call in the Start() method is not finding the troll or not finding the EndCollider component on it, resulting in it being null. And since you only search once at startup it will always be null.

This is probably because your troll is not yet created at the start of the scene.

If your intent is actually to make the monster appear when the player hit the collider, you should look at Prefab Instantiation.

http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

So in your OnTriggerEnter, you can simply instantiate a brand new monster:

public class EndCollider : MonoBehaviour {

public GameObject MonsterPrefab;

void OnTriggerEnter ()
{
    Instantiate(MonsterPrefab);
}

}
0
votes

One better way of communicating data between two scenes might be using PlayerPrefs:

//EndCollider.cs
public bool isShown
{
    get { return PlayerPrefs.GetInt("IS_SHOWN")==1? true:false; }
    set { PlayerPrefs.SetInt("IS_SHOWN", Convert.ToInt32(value));} 
}

//SlowDownRun.cs
if(PlayerPrefs.GetInt("IS_SHOWN")==1)  // isShown = true
{
    ...
}
0
votes
  • Make masterShow endcollider public
  • clean your start method.
  • Just drag and drop the troll object from hierarchy to your script.

    public class SlowDownRun : MonoBehaviour {

    public EndCollider MonsterShow;

    // Use this for initialization void Start () {

    }

    void Update() { if (MonsterShow.isShow == true) { float movementSpeed = 10f; transform.position += transform.forward * Time.deltaTime * movementSpeed; } }

    }