- Home /
Remmember the spawner
I asked this question before and I didn't get any answer or a tip for some days. I can't continue building the game without it:
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 have many of them. The code works fine when I have only one object that can spawn helicopters.
After I got an answer there I got these two codes:
on the spawner:
var chopper : Transform;
var spawnPoint : Transform;
var spawningEffect : Transform;
var isAlive = false;
function Update ()
{
if(!isAlive)
{
Spawn ();
}
}
function Spawn ()
{
isAlive = true;
var effect = Instantiate (spawningEffect, spawnPoint.transform.position, Quaternion.identity);
yield WaitForSeconds (4.0);
var spawnChopper = Instantiate (chopper, spawnPoint.transform.position, Quaternion.identity);
spawnChopper.SetSpawner(this);
}
On the helicoper:
var landingPlate;
function SetSpawner (spawner : GameObject)
{
landingPlate = spawner;
}
I'm getting this error: NullReferenceException: Object reference not set to an instance of an object.
What is the problem?
Answer by MibZ · Dec 14, 2012 at 05:34 PM
The "this" keyword references the script it is written inside of, not the GameObject.
//Change the code in spawner to this
spawnChopper.SendMessage ("SetSpawner", this.gameObject);
Now I get this error: $$anonymous$$issingFieldException: Field 'UnityEngine.Vector3.position' not found.
Answer by Bunny83 · Dec 15, 2012 at 10:13 AM
You included way too less information. We don't know how you spawn the chopper, also we don't know what spawnChopper is actually referencing, the spawner, or the spawnedChopper?
I would do something like this:
//Chopper.js
public var mySpawner : Spawner;
function Update()
{
// chopper stuff
if (ShouldFlyBack)
FlyTo(mySpawner.transform.position);
}
//Spawner.js
public var chopperPrefab : Chopper; // assign in the inspector
function Spawn()
{
var clone : Chopper = Instantiate(chopperPrefab, transform.position, transform.quaternion);
clone.mySpawner = this;
}
Now you can always reference back to the spawner script (and it's gameobject / transform) whenever you want by using the mySpawner reference in the chopper.
If you don't like this as public variable, you can make it private and create a setter:
//Chopper.js
private var mySpawner : Spawner;
function SetSpawner(aSpawner : Spawner)
{
mySpawner = aSpawner;
}
function Update()
{
// chopper stuff
}
//Spawner.js
public var chopperPrefab : Chopper; // assign in the inspector
function Spawn()
{
var clone : Chopper = Instantiate(chopperPrefab, transform.position, transform.quaternion);
clone.SetSpawner(this);
}
spawnChopper is the Chopper and landingPlate is the spawner. I will put more information if it not enough.
Both codes you gave me don't work, I get an error that there are no such types of varibles Spawner and Chopper: The name 'Spawner' does not denote a valid type ('not found'). Did you mean 'System.Runtime.Remoting.$$anonymous$$etadata.W3cXsd2001.SoapAnyUri'?
Your answer
Follow this Question
Related Questions
Editing in Animator removes Player tag 0 Answers
[js]NullReferenceException: Object reference not set to an instance of an object 1 Answer
Console throwing errors when trying to rename or highlight elements. 1 Answer
Hi I get an error code when i'm using my Javascript and I can't understand how to fix it... :( 1 Answer