I’m currently creating a script that allows me to spawn objects from a gameobject.
The script will spawn the objects by instantiating them, and then adding a force to their rigidbody to create movement.
To call their method, I am using the Invoke method in the start function.
I have 5 of these gameobjects, each with the same script.
And as you can understand, if I use Invoke(“Object1”, 1), it will invoke this function on ALL 5 of my game objects.
I do not want this, and I somehow want to make it that it will choose a random one of these gameobjects to invoke the function from.
Or if that is far to advanced, to maybe somehow tell it which gameobject to make it spawn from.
Would the best idea be to make this script single, say spawnScript1, spawnScript2, so on so on, and attach it to each of the gameObjects, and to Invoke the functions from each of the single scripts?
Or is there a better way of doing this by keeping the 1 script?
Thank you!
You don’t need to Invoke. When assigning a script to the object, they work independently for the most part . . .
Use SendMessage ( ) instead.
var ScriptName : Component ;
ScriptName = .......GetComponent ( ScriptName ) ;
ScriptName.SendMessage ( "FunctionName", {params} );
Hmmm SendMessage is quite cost effective, since this game is based around the iPhone.
Any other suggestions?
you should attach some code of the core lines that you are referring to.
Also using paragraphs is great when you’re talking about something so complicated!
It sounds like you are going through quite a lot of complicated stuff to invoke five functions to instantiate things at different times. You don’t really have to use the invoke function, I expect it is better suited to tasks like making a door close five seconds after you open it.
I would do something like this:
just set the five game objects as variables in the script that are supposed to invoke their method.for example drag them onto the Inspectoror call them by name in the script.
afterwards trigger one of each randomly :
var ST = spawn trigger: int = random.range (0,5);
trigger spawn trigger to run when you need to invoke a random object's method
if (ST ==0) {get gameobject 1's compenent script, trigger it's method.}
etc up to ST=4
Hmmm I think I understand haha. And yeah, I’ve been trying to put it into paragraphs, yet for some reason, each time I try to put a break between my sentences with the enter key, it posts without it?
I lied @ZoomDomain , I’m still super confused!