- Home /
Remmember from where it spawned.
I made some helicopters which spawns whenever another helicopter is destroyed. The helicopter follow me from above and when its run out of ammo it should fly back exactly to where it spawned. For this, I put a tag on some objects which can spawn them.
the problem is that when the helicopter spawns it can't choose the object that spawned it because i got many of them. The code works fine when I got only one object that can spawn helicopters.
What should I do to make the helicopter remmember from where it spawned?
Edit: Now I have these codes: on the helicopter:
var landingPlate;
function SetSpawner (spawner : GameObject)
{
landingPlate = spawner.transform.position;
}
The line for the distance is in the Update function.
On the spawner:
var spawnHelicopter = Instantiate (helicopter, spawnPoint.transform.position, Quaternion.identity);
spawnHelicopter.SendMessage ("SetSpawner", this);
I get this error after some changes (the codes above are the codes I have now): NullReferenceException: Object reference not set to an instance of an object.
What is the problem?
Up, I need an answer, I can't continue to build my game without it.
Answer by DannyB · Dec 07, 2012 at 07:50 PM
You could do this:
After the Spawner spawns the Helicopter, it will send a message to it:
helicopter.SendMessage( "SetSpawner", this );
Now, the Helicopter object will have a method that can accept this message, and will use it to remember the position of the spawner locally, in the helicopter:
public void SetSpawner( GameObject spawner ) {
originPosition = spawner.transform.position;
}
I use JS, not C#. I think the first code you gave me works on JS, the second code you gave me I need to change to:
function SetSpawner (spawner)
and the rest of it looks the same?
Yes, I believe so, perhaps:
function SetSpawner( spawner : GameObject )
or
function SetSpawner( spawner : Spawner )
I believe both should work
See http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html
The code works but now the helicopter don't know the distance between it and the spawner. This is importent because the helicopter's movement based on the distance from the spawner (flys up until it gets to the proper hight to fly forward).
Sorry for bad english.
What exactly doesn't work? Do you get an error? Are you sure the object spawner is valid?
spawner should be a global variable in your helicopter script.
Your answer
Follow this Question
Related Questions
Spawning random prefabs with the same tag 2 Answers
How do i make an object spawn another one? 1 Answer
Spawning in a specified location 2 Answers
Remmember the spawner 2 Answers
problem with set tag in unity network 0 Answers