- Home /
Simple array an spawning question
Having just a small amount of trouble with a code I have created.
This code creates an array of 5 game objects, and then assigns one of these random game objects to another variable, called randomSpawn. Each of these gameObjects has the same script on it, named spawnObjects. I am attempting to pick one of these game objects, and call one of its functions from the script.
When I do this, the same function is called for EVERY game object. Is it because they are all using the same script? Or because there is an error in my code?
var spawnPoints: GameObject[];
var i: GameObject;
private var randomSpawn:GameObject;
function Update ()
{
randomSpawn = spawnPoints[Random.Range (0,5)];
//print(randomSpawn);
}
function Start()
{
Invoke("MakeLevel", 3);
}
function MakeLevel()
{
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn1();
yield WaitForSeconds(3);
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
}
Answer by Montraydavis · Oct 14, 2012 at 02:00 PM
It's the Invoke, which calls for all objects of the same type.
var Object : GameObject = gameOBject.GetComponent ( blaa ) ;
Object.SendMessage ( "MakeLevel", 3 ) ; // Use this instead of Invoke ! :) Should work like a charm.
Answer by zillion · Oct 14, 2012 at 02:03 PM
Well, once I get this working, it will hopefully look something like this
function MakeLevel()
{
randomSpawn.gameObject.GetComponent(spawnObjects).SpawnFish1();
yield WaitForSeconds(3);
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn4();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn5();
yield WaitForSeconds(3);
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn1();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn4();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn3();
randomSpawn.gameObject.GetComponent(spawnObjects).Spawn5();
//So on so on, spawning more and more after a longer period of time.
}
Yeah, drop the Invoke, and use Send$$anonymous$$essage ( ) ins$$anonymous$$d, and the rest should be self-explanatory .
Answer by zillion · Oct 14, 2012 at 02:04 PM
I have heard from other students that SendMessage is very costly. I am creating this game for an iPhone. What is your suggestion on this?
Answer by zillion · Oct 14, 2012 at 02:08 PM
@Montraydavis - Tried your code just then, and doesn't seem to compile correctly. Never used SendMessage before.
Answer by Montraydavis · Oct 14, 2012 at 02:10 PM
@zillion : Here is an example of using SendMessage - http://docs.unity3d.com/Documentation/ScriptReference/Component.SendMessage.html
And if you don't want your CPU to pay those costs, why not call the function from the component directly ?
Your answer
Follow this Question
Related Questions
Calling random functions 4 Answers
Random spawn timer 1 Answer
How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers
Spawn object in random areas 2 Answers
Spawn random amount of gameobjects 2 Answers