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.