x


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.

But,

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

more ▼

asked Jan 14 '10 at 07:38 PM

peter gravatar image

peter
356 33 38 40

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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".

more ▼

answered Jan 14 '10 at 09:58 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 15 '10 at 12:41 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5078
x1673

asked: Jan 14 '10 at 07:38 PM

Seen: 1149 times

Last Updated: Feb 17 '10 at 12:43 PM