Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aronatvw · Jan 20, 2015 at 04:08 AM · arrayrandomarraylist

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;
     }


Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SeasiaInfotechind · Jan 20, 2015 at 05:06 AM 0
Share

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

screen shot 2015-01-20 at 10.33.10 am.png (20.9 kB)
screen shot 2015-01-20 at 10.32.39 am.png (17.2 kB)
avatar image aronatvw · Jan 20, 2015 at 05:23 AM 0
Share

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.

avatar image SeasiaInfotechind · Jan 20, 2015 at 05:36 AM 0
Share

So is your next variable assigned a value 2 in the inspector? Also mention line number where you are trying to print next.

avatar image Superrodan · Jan 20, 2015 at 06:34 AM 0
Share

Where is the next variable declared? I'm not seeing a public next variable.

avatar image skylem · Jan 20, 2015 at 10:20 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aronatvw · Jan 20, 2015 at 01:12 PM 0
Share

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.

avatar image aronatvw · Jan 20, 2015 at 01:30 PM 0
Share

@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.

avatar image Superrodan · Jan 20, 2015 at 08:16 PM 1
Share

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.

avatar image aronatvw · Jan 20, 2015 at 08:47 PM 0
Share

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!

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to check for GameObject is null in array with random 1 Answer

How to know what random number is chosen 2 Answers

¿how to take all values from an array without order? 2 Answers

Using arrays to randomly select an object 0 Answers

Random Range Seems... Unrandom 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges