Basically I have a player character object and when the player hits a button I want to replace that character object with a different character object. I have that working fine with this code:
spawnposition = GameObject.FindWithTag("player").transform.position;
Destroy (GameObject.FindWithTag ("player"));
GameObject newplayer = Instantiate(prefabtospawn, spawnposition, transform.rotation) as GameObject;
However, I have a camera and certain enemies whose movement is based on the player character and they have references to the player character set up as such:
_playerposition = GameObject.FindGameObjectWithTag("player").transform;
So when I execute the first set of code above, it works as intended, getting the correct spawn position, killing off the old object, spawning the new one, but then of course I get error messages because the camera / enemies are trying to access the old player position and that object no longer exists.
I’m not really sure what to do about this. I figure that I either…
A. Instead of killing the original object and creating a new one, just switch it out somehow (is this even possible?)
Or if that isn’t possible…
B. Find all references to the old object and set them to the new object. No idea how to do this though.
What would be the best way to handle this and where would I start?