- Home /
How do I get transform.localScale (C#) to apply to a clone generated by an object rather than the object the script is attached to?
I have an object to which a script is attached that generates enemies at a random time & place. I would also like to vary the size. I have seen the documentation on transform.localScale, but that just changes the size of the object the script is attached to, not the clones it generates. How do I get it to attach to the generated clone objects instead? Can I do this in script or do I need to give it various sizes of the enemy to generate from?
Answer by rutter · Feb 04, 2014 at 01:41 AM
Welcome to Unity Answers!
If you know which GameObject you're interested in, you can set its local scale easily enough.
You didn't mention a language, so I'll assume JavaScript:
var go : GameObject = ... //some GameObject
go.transform.localScale = ... //whatever you want
The hard part, then, is figuring out which GameObject you want to edit. Your scene could contain hundreds!
The best time to get a reference to a clone is right as its made. I assume you're using Instantiate to create the clone? It returns a reference to the cloned object. You can cache that reference:
//assumes you're cloning a GameObject
var clone : GameObject = Instantiate(...) as GameObject;
clone.transform.localScale = ...
Depending on what exactly you're trying to do, you can edit the clone immediately, or you can keep track of it for a while and edit it later. In some cases, it might be useful to keep a collection of clones for later reference.
Thankyou ^_^
it was C#, but the realising I need to make a variable for each instance as it's made to be able to reference it was enough. I'm very new at this. Thankyou ^_^
Your answer
Follow this Question
Related Questions
Instantiate Prefab Array with ID 2 Answers
how do I reference a clones of a instantiated game object and only the clones In a script? 1 Answer
How do I check the variable of a specific clone? 1 Answer
Slowly scale object to original size on instantiate 0 Answers
Just curious, assigning a clone multiple times, how does it work internally? 1 Answer