- Home /
Creating objects and ordering them to act
For some reason I'm having substantial problems simply instantiating a GameObject of a certain type, and then making it perform actions. I think the problem is that I'm missing some kind of underlying fundamentals on how Unity behaves.
I have a GUI button, when clicked it calls the ButtonPressed_Green() function on the target, the target being an instance of the character in game.
function ButtonPressed_Green() { var blob = Instantiate(blobFab, Vector3(transform.position.x, transform.position.y+5, transform.position.z), Quaternion.identity); blob.movement.direction.x = 1; blob.SendMessage("ToggleWalking");
}
This code should create an instance of blobFab, a variable on my script that is set to the prefab BlobParent. It does that part fine, but the second line "blob.movement.direction.x" does not work.
Movement is a variable of the BlobController script (this same script, this blob is essentially duplicating itself) that refers to the BlobMovement class with some variables for x/y/z. How do I access these variables using the GameObject reference, blob, itself?
Answer by Polatrite · Oct 19, 2010 at 06:56 PM
As always, I find the answer 5 minutes after asking, despite tinkering for ages beforehand!
I must use GetComponent() to retrieve the actual script object, then reference those variables through the script object.
function ButtonPressed_Green() { var blob = Instantiate(blobFab, Vector3(transform.position.x, transform.position.y+5, transform.position.z), Quaternion.identity); var blobmovement : BlobController; blobmovement = blob.GetComponent("BlobController"); blobmovement.movement.direction.x = 1; blob.SendMessage("ToggleWalking");
}
Answer by Bravini · Oct 20, 2010 at 01:17 AM
or you could make it into a list of blobs as the new blob will receive the name (Clone) after it, and listing would make you able to synchronize, halt or find em easier.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080146im_/https://answers.unity.com/themes/thub/images/avi.jpg)