problem with lists
In my code i am creating a list of integers that should stop at 6 and then generate certain items depending on the numbers in said list but at the moment the list is not reaching 6 and then generating an object and i cant figure out why it never gets to 6 (because there is variation as it will occupationally put 2 or 3 objects in the list) and how is passing the if statement and generating an object, here it the code in question (sorry its a bit long):
List<int> roomList = new List<int> ();
// Use this for initialization
void Start ()
{
roomPosition = Random.Range (0, 20);
if (roomPosition == 5 || roomPosition == 10 || roomPosition == 15) {
Start ();
} else {
roomList.Add (roomPosition);
}
maingeneration ();
}
void maingeneration ()
{
listLength = roomList.Count;
if (listLength > 6) {
direction = Random.Range (0, 4);
if (direction == 1) {
roomPosition = roomList[roomList.Count - 1] + 1;
if (roomPosition == 5 || roomPosition == 10 || roomPosition == 15) {
roomList.Clear ();
Start ();
}
if (roomPosition > 19) {
roomList.Clear ();
Start ();
}
if (roomPosition < 0) {
roomList.Clear ();
Start ();
} else if (roomList.Contains (roomPosition)) {
roomList.Clear ();
Start ();
} else {
roomList.Add (roomPosition);
maingeneration ();
}
}
if (direction == 2) {
roomPosition = roomList[roomList.Count - 1] - 1;
if (roomPosition == 5 || roomPosition == 10 || roomPosition == 15) {
roomList.Clear ();
Start ();
}
if (roomPosition > 19) {
roomList.Clear ();
Start ();
}
if (roomPosition < 0) {
roomList.Clear ();
Start ();
} else if (roomList.Contains (roomPosition)) {
roomList.Clear ();
Start ();
} else {
roomList.Add (roomPosition);
maingeneration ();
}
}
if (direction == 3) {
roomPosition = roomList[roomList.Count - 1] + 5;
if (roomPosition == 5 || roomPosition == 10 || roomPosition == 15) {
roomList.Clear ();
Start ();
}
if (roomPosition > 19) {
roomList.Clear ();
Start ();
}
if (roomPosition < 0) {
roomList.Clear ();
Start ();
} else if (roomList.Contains (roomPosition)) {
roomList.Clear ();
Start ();
} else {
roomList.Add (roomPosition);
maingeneration ();
}
}
if (direction == 4) {
roomPosition = roomList[roomList.Count - 1] + 5;
if (roomPosition == 5 || roomPosition == 10 || roomPosition == 15) {
roomList.Clear ();
Start ();
}
if (roomPosition > 19) {
roomList.Clear ();
Start ();
}
if (roomPosition < 0) {
roomList.Clear ();
Start ();
} else if (roomList.Contains (roomPosition)) {
roomList.Clear ();
Start ();
} else {
roomList.Add (roomPosition);
maingeneration ();
}
}
}
else
{
Instantiate (full_box);
/*if (roomList [0] - roomList [1] == 1)
{
//Instantiate (full_box);
}*/
}
}
}
Any help would be greatly appreciated, thanks.
Could you please clarify your intentions? Which variables are you trying to fill with data, and what kind of data? Are you trying to fill roomList with 6 unique random room numbers ranging from 0 to 20, and then call the maingeneration() method?
Right now, your code only has one way of execution. First, it creates a random number from 0 to 20 (excluding 20). If that number is 5 or 10 or 15, you are calling the function again, and you are generating the number again. When the generated number is not 5 or 10 or 15, you are adding it to the list, and call maingeneration. The method simply jumps to the 'else' statement at the end of the functon, because the length of the list is always 1. Then it instantiates the 'full_box' object.
I am trying to add 6 unique integers between 0 to 20 (excluding 20) that are not 5, 10, 15, the first number is a number generated between 0 to 20 (excluding 20) and only need to be checked if its 5 10 or 15 as it cant be over 19, under 0 or already in the list, then it would go to main generation, check the list length and see its less than 6 and continue and for the other 5 numbers i was hoping it would add an int that is either, 5 greater / 5 less / 1 greater / 1 less than the previous number in the list (hence the direction number), check if its not 5, 10 or 15, then check if its greater than 19, then check if its below 0, then check if its already in the list, finally if it passed all these it would be added to the list and the cycle would repeat and after 6 repetitions of the cycle it would go to the bottom part of the statement and instantiate different objects depending on what is in the list (although i haven't done that bit yet so i just set it to instantiate a full box in its place), so how do i stop the list length always being 1? (also i did change a lot of the 'if's' to 'else if's' but it didn't solve the problem)
Your answer
Follow this Question
Related Questions
UNITY - How to sort items in list with animation step by step (to up/down) 0 Answers
Help with rotation please 2 Answers
Infinite Time inbetween if statements? 2 Answers
if statements are running when they are not suppose to be 1 Answer
Trying to program two buttons to appear when the player in my game dies 0 Answers