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?

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.