In the video at 1:47:00, we need to check for null. How to do that?
I am a beginner and have nowhere found a correct answer to this question. But I figured it out. Here for all who are still looking for …
void Start()
{
//This is the original line from the tutorial.
//playerTransform = GameObject.FindWithTag("Player").transform;
//Replaced by the class "FindTarget" which checks for null.
FindTarget();
}
//Here is check for null.
//If the GameObject Player does not exist, the playerTransform uses the GameController
private void FindTarget()
{
if (GameObject.FindWithTag("Player") == null)
{
playerTransform = GameObject.FindWithTag("GameController").transform;
return;
}
playerTransform = GameObject.FindWithTag("Player").transform;
}
//So that the existing enemies notice that you are dead,
//need playerTransform a update every frame.
void Update()
{
FindTarget();
}