- Home /
Ball Falling Game.
I've made a ball falling game and i'm trying to make it random so: a ball falls with gravity and has to get to the bottom but if you land on a platform you bounce, I just can't get the platforms to be random. this is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawner : MonoBehaviour
{
public GameObject obstacle1;
public GameObject obstacle2;
public GameObject obstacle3;
public GameObject obstacle4;
public GameObject obstacle5;
public GameObject obstacle6;
public static int random;
private void Start()
{
random = Random.Range(0, 6);
}
private void Update()
{
if (random == 0)
{
Instantiate(obstacle1, transform.position, Quaternion.identity);
}
if (random == 1)
{
Instantiate(obstacle2, transform.position, Quaternion.identity);
}
if (random == 2)
{
Instantiate(obstacle3, transform.position, Quaternion.identity);
}
if (random == 3)
{
Instantiate(obstacle4, transform.position, Quaternion.identity);
}
if (random == 4)
{
Instantiate(obstacle5, transform.position, Quaternion.identity);
}
if (random == 5)
{
Instantiate(obstacle6, transform.position, Quaternion.identity);
}
}
}
Answer by Klarzahs · Jan 10, 2019 at 11:37 AM
Hi,
You dont update your random variable, you only assign it once in Start(). This way, every time the update gets called, your variable still has the same value and will always choose the same branch.
Answer by famousfive1 · Jan 10, 2019 at 11:43 AM
Well the start method only runs once. So the number is generated only once and is stored. So you get the same platform again and again. Put the line of code which is inside start() at the inside update() before everything else. That should fix the problem.
i didn't see that $$anonymous$$larzahs already answered it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612185501im_/https://answers.unity.com/themes/thub/images/avi.jpg)