How can instantiate some clone of an object of random sizes (for example between 1 and 5)?
Off the top of my head:
objectSizeMax: int =5;
myObject:GameObject;
function MakeNewObject(){
var numRandom:int = (Random.value*objectSizeMax)+0.5; // or you could use Random.Range(0,objectSizeMax)+0.5;
var objectMade: GameObject = Instantiate(myObject,transform.position,transform.rotation);
objectMade.transform.localScale = Vector3.one * numRandom;
}
GameObject InstantiateRandomScale(GameObject source, float minScale,
float maxScale)
{
GameObject clone = Instantiate(source) as GameObject;
clone.transform.localScale = Vector3.one * Random.Range(minScale, maxScale);
return clone;
}
Then use it as
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject clone = InstantiateRandomScale(prefab, 1, 5);
// set up clone further.
}
}
If you want to scale it with respect to the original scale, you can use Vector3.Scale.
Anyhow, the only thing worthwhile different than creating any prefab is the one line of code:
transform.localScale = Vector3.one * Random.Range(minScale, maxScale);
or use this to scale it with regard to your current scale:
transform.localScale *= Random.Range(minScale, maxScale);