- Home /
Problems with array "shuffle bag"
Hi guys, so.. I'm trying to create an array (dialogArray) to store random values (randomDialog) BUT only once for each value, that way the random value never repeat itself. What should come out of it is a value to be used on another part of the script to choose a dialogue (actualDialog).
But it seems that the random values are never stored (dialogArray.Add(randomDialog);) and so the code keeps running the same numbers as if they were never picked before.
Thanks!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class init : MonoBehaviour {
private int randomDialog;
public List<int> dialogArray;
private int actualDialog = 0;
void Awake()
{
Dialoguer.Initialize();
}
// Use this for initialization
void Start ()
{
}
void getRandomDialog()
{
randomDialog = Random.Range(0,11);
}
// Update is called once per frame
void Update ()
{
dialogArray.Add(40);
if(Input.GetKeyDown("space"))
{
getRandomDialog();
Debug.Log(randomDialog);
for( int i=1; i < 8; i++)
{
if(randomDialog == dialogArray[i])
{
Debug.Log("equal");
return;
}
else if(randomDialog != dialogArray[i])
{
break;
}
}
dialogArray.Add(randomDialog);
Debug.Log("Pass");
actualDialog = randomDialog;
Debug.Log(actualDialog);
}
}
Answer by jackdracon · Apr 12, 2014 at 06:18 PM
So,try this one:
void Randomize_Cannons()
{
var current_Cannons = Cannons;
List<GameObject> randominzed_Cannons = new List<GameObject>();
while(current_Cannons.Count > 0)
{
var _ind = Random.Range(0, current_Cannons.Count);
randominzed_Cannons.Add(current_Cannons[_ind]);
current_Cannons.RemoveAt(_ind);
}
Cannons = randominzed_Cannons;
}
Why you don't try to reset your logic after some time? Or some 'event', 'trigger' inside the game?
Your answer
Follow this Question
Related Questions
Randomly Choosing two objects from an array and switching their positions 3 Answers
RandFuncs.Shuffle question 0 Answers
How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer
how to check for GameObject is null in array with random 1 Answer
How to know what random number is chosen 2 Answers