Spawn Objects and use Variables

Hello,

im trying to create a script to spawn GameObjects and want to move them then.

For this case i need to assign variables to the created objects.
Later i want to get the values of these variables to move the objects with the correct speed.
Normally i created a class, assigned variables to them, and could acess them by: " Object.variableName"

But i didnt figured out how that works in unity.

Her is an example of my code:

var myObject : GameObject = GameObject.FindGameObjectWithTag("inEditorCreatedSprite"); 
var spawnExampleObject : GameObject = Instantiate (myObject, spawnPosition, Quaternion.identity );

I want to givem them now “speed” and other variables, but i dont know how to assign / acess them to my created object.
Is "cloning"with instantinate even the correct method ? Outside of unity i created classes and children for these purposes…
Can somebody tell me how that works, or if there is an better way to do it?

// How about this

GameObject obj = Instantiate(myObject, spawnPosition, Quaternion.identity) as GameObject;

//obj.GetComponent<YourScriptToBeAccessed>().Variable = ???

Yes, but it’s highly likely you should create prefabs rather than duplicating an object in the scene. Use GetComponent to access components on instantiated objects.

–Eric

Ok, didnt knew that i have to acess the script on the object first, and then the variable, thank you guys :slight_smile:

In the description “prefab” sounds more or less like a class, i will use that too.

–Eric