The first set of code below works. The second set is able to access the game object Enemy but doesn’t access the EnemyScript.
How do I write that script so I don’t have to drag and drop into the inspector every time I build a scene. Meaning, I want Enemy and Enemy Script to not be public be reference in the start function, but i only get a Null Reference error for the EnemyScript.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class GameControllerTest : MonoBehaviour {
public GameObject Enemy;
public EnemyController EnemyScript;
public bool PlayerInSightTEST;
void Start()
{
Enemy = GameObject.FindGameObjectWithTag("Enemy");
EnemyController EnemyScript = Enemy.GetComponent<EnemyController>();
}
void Update()
{
if (EnemyScript.playerInSight)
{
PlayerInSightTEST = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class GameControllerTest : MonoBehaviour {
GameObject Enemy;
EnemyController EnemyScript;
public bool PlayerInSightTEST;
void Start()
{
Enemy = GameObject.FindGameObjectWithTag("Enemy");
if (Enemy != null)
{
EnemyController EnemyScript = Enemy.GetComponent<EnemyController>();
if (EnemyScript != null )
{
Debug.Log("EnemyScript found");
}
else
{
Debug.Log("EnemyScript NOT found");
}
Debug.Log("Enemy object found");
}
else
{
Debug.Log("Enemy object NOT found");
}
}
void Update()
{
if (EnemyScript.playerInSight)
{
PlayerInSightTEST = true;
}
}
}