public variable gameobject
I have done public GameObjects in many scripts no problem but for some reason this one isn't working. If I substitute the actual name of the enemy in the field the script works. but when I make the enemy a variable and drag the enemy I want into that slot I get these errors "Object reference not set to an instance of an object" As I said, when I substitute the actual enemy name the script works and disables that enemies "enemybehaviour destruct" script. The game will play and I don't get those errors until the player hits that trigger zone. this script doesn't work
public GameObject Enemy;
private MonoBehaviour Attacks;
void Start () {
Attacks = GameObject.Find("Enemy").GetComponent<EnemyBehaviourDestruct>();
}
void OnTriggerEnter (Collider other) {
if(other.tag == "Player")
{
Attacks.enabled = true;
}
}
}
this script DOES work
private GameObject MadScientist;
private MonoBehaviour Attacks;
void Start () {
Attacks = GameObject.Find("MadScientist").GetComponent<EnemyBehaviourDestruct>();
}
void OnTriggerEnter (Collider other) {
if(other.tag == "Player")
{
Attacks.enabled = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612081325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GameObject fields evaluate to null, but they are actually assigned and working 1 Answer
Public variables acting like private ones! 0 Answers
How to use a public int in another script? 1 Answer
not a question, just advice on public variables 1 Answer
public int variable not updated in inspector for two scenes in Unity 0 Answers