- Home /
How to spawn random objects with differents vector 3?
[SerializeField] private GameObject SpikeHolder; [SerializeField] private GameObject Missile; [SerializeField] private GameObject MovingSpikeUp; [SerializeField] private GameObject MovingSpikeDown;
int randomObject;
void Start()
{
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
IEnumerator Spawner()
{
yield return new WaitForSeconds(1);
Vector3 temp = SpikeHolder.transform.position;
temp.y = Random.Range(-2.5f, 2.5f);
Vector3 temp2 = Missile.transform.position;
temp2.y = Random.Range(-3.5f, 3.5f);
Vector3 temp3 = MovingSpikeUp.transform.position;
Vector3 temp4 = MovingSpikeDown.transform.position;
randomObject = Random.Range(1, 4);
if(randomObject==1)
{
Instantiate(SpikeHolder, temp, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else if(randomObject==2)
{
Instantiate(Missile, temp2, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else if(randomObject==3)
{
Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else
{
Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
My code have a problem, anyone can tell me how i can spawn random objects with different vector3????
Answer by Shippety · Oct 11, 2017 at 06:51 PM
Think your problem here is probably in the if / else statements. Random.Range() returns a float between the two numbers you set (https://docs.unity3d.com/ScriptReference/Random.Range.html) so only the default on your if / else statements will be true. What you could do is Mathf.RoundToInt() your randoms (https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html) so that they're integers. Could look something like this:
int randomObject;
void Start()
{
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
IEnumerator Spawner()
{
yield return new WaitForSeconds(1);
Vector3 temp = SpikeHolder.transform.position;
temp.y = Random.Range(-2.5f, 2.5f);
Vector3 temp2 = Missile.transform.position;
temp2.y = Random.Range(-3.5f, 3.5f);
Vector3 temp3 = MovingSpikeUp.transform.position;
Vector3 temp4 = MovingSpikeDown.transform.position;
// THIS PART!!
randomObject = Mathf.RoundToInt(Random.Range(1, 4));
if(randomObject==1)
{
Instantiate(SpikeHolder, temp, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else if(randomObject==2)
{
Instantiate(Missile, temp2, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else if(randomObject==3)
{
Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
else
{
Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
InvokeRepeating("Spawner", 4f, 1f);
StartCoroutine(Spawner());
}
Although I didn't try that code out myself, that's the idea of what you wanna do. Not totally sure what you want to happen with the temp vectors, if you want them to be whole numbers you could do the same thing on each axis.
Random.Range(int m, int n) returns an int from 'm' to 'n-1' so that line should be good.
about the vector, i want each game object have their own vector, i saw so many excercise on web but all of them are spawn objects with same vector.
Answer by gnp89 · Oct 11, 2017 at 07:04 PM
You're calling InvokeRepeating multiple times, starting the coroutine multiple times, quite unpredictable :)
Just calling InvokeRepeating solves your problem of spawning every X seconds.
You don't specify exactly what your problem is. Are you having an exception? Nothing happens? Is the first object instantiated but then it stops?
The following code should work if all your variables are assigned. You can make it more elegant by using a switch statement, and using arrays for holding the prefabs and their default positions.
int randomObject;
void Start()
{
InvokeRepeating("Spawner", 4f, 1f);
}
void Spawner()
{
Vector3 temp = SpikeHolder.transform.position;
temp.y = Random.Range(-2.5f, 2.5f);
Vector3 temp2 = Missile.transform.position;
temp2.y = Random.Range(-3.5f, 3.5f);
Vector3 temp3 = MovingSpikeUp.transform.position;
Vector3 temp4 = MovingSpikeDown.transform.position;
randomObject = Random.Range(1, 4);
if(randomObject==1)
{
Instantiate(SpikeHolder, temp, Quaternion.identity);
}
else if(randomObject==2)
{
Instantiate(Missile, temp2, Quaternion.identity);
}
else if(randomObject==3)
{
Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
}
else
{
Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
}
at the first time all of my objects were spawn and them it stop. I don't know what happen with my code