Follower script stops working after death of target. How to re-assign the target at respawn?

Hi everyone!

I am new to Unity and to coding in general and I am already having a lot of fun making my little game!

I followed a (great) Youtube tutorial for a side view platformer and I was trying to add something more personal with a light following my player. For that I made a Follower script (below) which works fine until the player dies and respawns, at which point the light doesn’t follow the player anymore and I get an error message saying:

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Follower.Update () (at Assets/Scripts/Follower.cs:10)

using UnityEngine;
using System.Collections;

public class Follower : MonoBehaviour {
	public GameObject targetToFollow;

	public Vector3 offset;

	void Update() {
		targetToFollow =  GameObject.FindWithTag(targetToFollow.tag);
		transform.position = targetToFollow.transform.position + offset;
	}
}

To kill the player I use this function:

	public void Die(){
		Destroy (gameObject);
	}

And to respawn the player:

private void SpawnPlayer(Vector3 spawnPos) {
	currentPlayer = Instantiate(player, spawnPos, Quaternion.identity) as GameObject;
	cam.SetTarget(currentPlayer.transform);
	}

After searching online for a bit I couldn’t figure a way to fix it which is why I am putting my hopes in your crafty hands :slight_smile:

  • So first of all is my approach valid
    or should I revise everything?
  • Secondly could you help me finding a
    solution to the problem or point me
    towards a source where I can learn
    how to do what I am trying to
    achieve?

Thanks in advance and cheers!

Pretty easy if your player has a specific script attached (maybe a Player script). There’s an error on line 10 of your follower - each frame you’re saying ‘find the target by asking the target what its tag is and search for it’. That’ll have no effect whatsoever initially, until your target dies and it’ll start giving you null reference errors.

How about in your update:

//if I don't have a target, try and find one by searching for a game object with your player script attached. 
if(targetToFollow == null)
    targetToFollow = FindObjectOfType<Player>();

//if I STILL don't have a target there's nothing I can do so bail!
if(targetToFollow == null)
    return;

Note: That assumes your player has a ‘Player’ script attached.

You could use FindWithTag instead of FindObjectOfType, but you’ll have to write in the correct tag, not try and read it from the target object.

I wouldn’t worry too much about the performance of the Find functions for now. It’s bad practice to call them every frame (they aren’t cheap) but it’ll be fine if you just do it when you need to find a new target.

More efficient alternatives involve more complex systems where you maintain some kind of ‘spawner’ that knows about what’s going on in the game so provides a fast way of finding stuff. But why take a complex solution to simple problem :slight_smile:

What if you didn’t destroy the player? Just disabled the gameobject, re-positioned, and reactivate it?

Otherwise, you’re going to need to put a check for if that player variable is null. Then you could have the player script or the spawner script or whatever reassign that variable when the player object is instantiated.

Wow thank you so much for all the answers, it looks like there is a great community here :slight_smile:
I’ll test the different solution and get back to you.
Thanks again!