- Home /
My loop keeps producing the same wrong result :,,(
This loop has Random features yet ALWAYS returns 2 for my variable 'next'. Too late to keep mind f'ing myself, hoping I could get a second pair of eyes! I print 'next' and 2 wont even show up in the console but does in the inspector. This is the only method I am using 'next' in!
void Start () {
var testy = new ArrayList();
testy.Add(8);
testy.Add(4);
testy.Add (6);
testy.Add (2);
print ("Random from arr = " + RandomRoom(testy));
}
public int RandomRoom(ArrayList arr)
{
int ret = 0;
int temp = 0;
int ran = 0;
var ranNext = new ArrayList();
int length = arr.ToArray().Length;
int pick = Random.Range(1, length);
print ("length = " + length );
print ("pick = " + pick);
int count = 0;
for(int i = 1; i <= pick; i++)
{
ran = Random.Range(0, length);
temp = int.Parse(arr[ran].ToString());
ranNext.Add(temp);
next = temp;
print (temp);
if(temp == 8)
{
ret += 8000;
}
if(temp == 4)
{
ret += 400;
}
if(temp == 6)
{
ret += 60;
}
if(temp == 2)
{
ret += 2;
}
arr.RemoveAt(ran);
length = arr.ToArray().Length;
}
//next = int.Parse(ranNext[Random.Range(0,ranNext.ToArray().Length)].ToString());
return ret;
}
Hii,
I implemented your code and its returning different values , screenshot is attached. Unable to find whats troubling you. Please explain bit more about your problem.
[1]: /storage/temp/39157-screen+shot+2015-01-20+at+10.32.39+am.png
Try printing 'next' after assigning it, it's always 2 even if two is not in the array list. I actually haven't print 'next' but I'm going by the public variable in my inspector.
So is your next variable assigned a value 2 in the inspector? Also mention line number where you are trying to print next.
Where is the next variable declared? I'm not seeing a public next variable.
i experienced something similar using start to create random values appears to provide the same random values perhaps if u create a function through update this way the values would randomize continuously.
Answer by SnStarr · Jan 20, 2015 at 11:06 AM
next = temp;
print (temp);
If next is set to 2 in the inspector, and you are making Next = temp then printing temp, its gonna print 2 all the time. You need to declare in your code what exactly next is supposed to be. There is no public declaration for the next variable.
I'm sorry let me clarify, I'm on mobile so its a pain to type a ton. Next is a public int declared outside of any function. It does not equally anything until this method. That being said in the inspector next is always 2 no matter what numbers are chosen. I have even tried assigning next in the if statements for example if ( temp == 8) next = 8.
I did just think maybe printing next inside and outside of the loop may give me some insight.
@snstarr what you said would only be true if I wrote temp = next, which I never do. Also I dont assign next as anything I declare it outside of any function, public int next. That's it. Second guesting myself I've even searched through my script for next just to make sure it's not used anywhere else.
Honestly since I don't have access to your code base I only have suggestions on how to debug this yourself. How I would go about doing this is putting this code in the for loop:
Debug.Log(string.Format("On loop {0} next was {1}",i,next));
right after when you print temp. Then a:
Debug.Log(string.Format("After the for loop next is {0}", next));
after the for loop is complete.I'm curious if next is returning to 2 after the loop or if it staying 2 throughout. The code should run fast enough that if its returning to 2 after the loop you might never see it change in the inspector.
Seeing where it is being set to 2 might give you a better idea of how to narrow down the problem.
Yea this is exactly what I was going to try tonight. To be honest all you need to do to simulate how I'm using this code is put public int next; in a new script above start and the rest of the code in start and attach to an object. But thanks for the advice, so happens it was my next step, I'll report my findings!
Answer by aronatvw · Jan 21, 2015 at 03:49 AM
Well it is official guys I printed inside and out of the loop and it is definitely human error!!! It looked as if the loop was being called twice so I started searching. The randomRoom function was being called by another function I didn't think I was invoking. Sorry for all the confusion I would have posted more info but it's kind of hard when the script I am writing is nearing 900 lines. So long and thanks for all the fish!