I found the answer myself
how to scale an object on instantiate with a random value
I am trying to spawn birds with a random scale which works but I can only scale the x y and z individual and not the xyz at the same time (with the same random value)
this is my script:
using UnityEngine; using System.Collections;
public class BirdScript : MonoBehaviour { public float wait; public GameObject Bird; public Vector3 spawnValues;
void Start ()
{
StartCoroutine ( spawnBirds() );
}
IEnumerator spawnBirds()
{
for(int i = 0 ; i < 2 ; i++)
{
Vector3 spawnPosition = new Vector3 (spawnValues.x ,Random.Range (8f, 25f) , spawnValues.z);
Instantiate (Bird, spawnPosition , Quaternion.identity);
Vector3 scale = Bird.transform.localScale;
scale.x = Random.Range (1f, 5f);
Bird.transform.localScale = scale;
i = 0;
yield return new WaitForSeconds (wait);
}
}
}
any help would be greatly appreciated.
Answer by Eric5h5 · Sep 30, 2015 at 01:09 PM
You need to scale the object you're instantiating, not the prefab. Instantiate returns a reference to that object, so you assign it to a variable. In any case, simply assign the same value to x, y, and z.
But if I do that they wont have the same random value, x might be 1 while y could 5 I need the xyz to get the same random value
So just use Random.Range once and apply that to them all.
and that is what I cant get to work and am asking help about, how do I use it once to apply it to them all?
Well, I honestly have no idea what you mean, but I made it work by simply making the X have a random value and making the Y and Z equal to the X
Follow this Question
Related Questions
I can not modify localscale while I'm playing because of the Animator. 1 Answer
Use Different Gameobjects based on Local Scale? 0 Answers
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
continuously spawning and causes spawned objects to overlap 0 Answers