- Home /
Random number exclusion works only once [C#]
So for the game i'm making i want to spawn a score box randomly at any one of my preset spawn locations, however because i don't want the box to spawn at the same position as the previous one i have tried to exclude the last generated number. Sadly, this only works once, if it even works of all of which i'm not certain. Does anybody know how to fix this?
CODE: public class gameSystems : MonoBehaviour { public float lastNumberGenned; public float rdmFloat; public Transform boxToSpawn;
public float score;
public AudioClip pickupAudio;
void Awake () {
spawnBox();
}
void AddScore(float scoreAdded)
{
audio.PlayOneShot(pickupAudio);
score += scoreAdded;
}
void genRandomNumber ()
{
rdmFloat = Random.Range(1, 10);
spawnBox();
}
void spawnBox()
{
if (rdmFloat == lastNumberGenned)
{
genRandomNumber();
return;
}
if (rdmFloat != lastNumberGenned)
{
if (rdmFloat == 1) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn1").transform.position, Quaternion.identity); }
if (rdmFloat == 2) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn2").transform.position, Quaternion.identity); }
if (rdmFloat == 3) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn3").transform.position, Quaternion.identity); }
if (rdmFloat == 4) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn4").transform.position, Quaternion.identity); }
if (rdmFloat == 5) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn5").transform.position, Quaternion.identity); }
if (rdmFloat == 6) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn6").transform.position, Quaternion.identity); }
if (rdmFloat == 7) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn7").transform.position, Quaternion.identity); }
if (rdmFloat == 8) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn8").transform.position, Quaternion.identity); }
if (rdmFloat == 9) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn9").transform.position, Quaternion.identity); }
lastNumberGenned = rdmFloat;
}
}
}
As far as i can see your spawnBox() method executes only once inside the Awake() method. In order to randomly execute your method more than once you have to decide some kind of logic to drive it. Can you provide more details how you spawn your prefab in order to get an accurate result?
okay for some reason this excluded some code, i've edited it now (probably by the time you read this i have)
There is not enough information to help you out. $$anonymous$$aybe you could write how and when exactly you want your prefab to spawn.
holy shit i am an idiot. Looking back at code i forgot that i NEVER called the function outside of Awake()... i thought i called it in another script..
Answer by RoboticSarcasm · Nov 07, 2014 at 06:41 PM
I am an idiot. Forgot to call the void from the script i wanted to call it...........
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to slow down random enemy spawn? 1 Answer
I can't get Unity to recognise Random.Range C# 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer