c# - How can i keep track of which randomly selected model was generated so it won't repeat again? -
this code generated random game object because there 9 game objects randomly generated object keeps re-generating more 1 time. how can restrict that, , have 1 game object generated once ?
public gameobject[] models; public static gameobject currentpoint; int index; public static string randomname; public audiosource findthenumber; public void playnumbers() { models = gameobject.findgameobjectswithtag ("numbers"); index = random.range (0,models.length); currentpoint = models [index]; randomname = currentpoint.name; print ("trackable " + randomname); findthenumber.play (); currentpoint.getcomponent<audiosource> ().playdelayed(2); }
if i've understood correctly, how keeping track of selected game objects in separate list.
public static list<gameobject> models; public static list<gameobject> selectedmodels = new list<gameobject>(); public static gameobject currentpoint; int index; public static string randomname; public audiosource findthenumber; public static random random = new random(); public void playnumbers() { models = gameobject.findgameobjectswithtag("numbers").except(selectedmodels).tolist(); if ((models == null) || (!models.any())) { console.writeline("no new game objects"); } else { index = random.next(models.count); currentpoint = models[index]; randomname = currentpoint.name; print ("trackable " + randomname); findthenumber.play (); currentpoint.getcomponent<audiosource> ().playdelayed(2); selectedmodels.add(currentpoint); } }
Comments
Post a Comment