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();
}
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.
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.
}
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?
@Montraydavis - Tried your code just then, and doesn’t seem to compile correctly. Never used SendMessage before.
@zillion : Here is an example of using SendMessage - Unity - Scripting API: Component.SendMessage
And if you don’t want your CPU to pay those costs, why not call the function from the component directly ?
@Montraydavis - Tried dropping the Invoke altogether, and came up with this: randomSpawn.gameObject.SendMessage(“Spawn1”);
Yet, that is still refusing to work, and the Spawn1 function is being called from all 5 of my spawn points.
What do you mean call the function from the component directly? I have the 5 spawn points all using the same script, and somehow, if I did it from that main script, I would need to create some code that calls the functions, but only for one of the gameobjects at a time.