look at script does not work when the game object is spawned instanced

My object shoots at player just fine using the look at command and using the : var target : Transform; Where target is the game object that's being targeted.


When shoot object is destroyed and then respawned (instantiated) the target variable is not active. How do I make the target variable the same when it's instantiated.

Example: object shoots at target, object dies, object respawned but shoots wrong

The target variable in your LookAt monobehaviour needs to somehow be updated to the new gameobject. One of the ways to achieve this is to make the code that does the respawning, also inform the lookat script about the "new thing to look at".

The variable will be empty as it is a reference to a gameobject that's not part of the prefab. A simple solution is to set the variable through code, this would give something like this:

var target : Transform;

function Start() {
    target = GameObject.FindWithTag("Player").transform;

Of course, this assumes you gave the player Tag Player and that you're interested in the transform of the player. If you want to store the GameObject of the player, replace `Transform` with `GameObject` and remove the `.transform`.