• 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 /
  • Help Room /
avatar image
Question by CheezyTatoSoup · Feb 09, 2021 at 08:13 PM · arraysstringrandom.rangeintindexof

C# Having troubles with creating/referencing a random list.

Basic Rundown is, I'm creating a random X and Y position based around an 2D image array to create the possible choices in game for the player. But i want to check the list as it is being made, to make sure that the new possibility is NOT in the list, to avoid Repeat options.

Here is what i have.

  using System;  
            int[] listX;  new int[5]
            int[] listY = new int[5];  
            int[] ranXY = new string[5];
 for (int i=0; i<5;i++){  
                int ranX = Random.Range(0,3);            //Makes random X,Y coords
                int ranY = Random.Range(0,2);  
                ranXY[i] = ""+ ranX+ ranY;              // Combines the 2 into a string for later verification
                int ranXY= Array.IndexOf(ranXY, ranXY[i]); // IndexOf to find if new XY exists in list
                if (ranXY !=0 |... | ranXY!=4)     // checks if the new item is in the list already
               {                                       //If successful, Add new XY to list of possibilities.
                     listX[i] = ranX;  
                     listY[i] = ranY;  
               }  
                 else{ i-=1;}
           }

Based on what i have here, it should create a set of random numbers in 2 lists, save each list to a string combined, check the list of combined XY coords, for the current random selection, and if it doesn't find the combo, adds it to the current position. If it does find the combo, repeat the process for the same array position until it finds a new combination.

The issue i am having, is i still end up with Repeat items in my list.

If i change if (ranXY!=0) etc. to if (ranXY= -0) it freezes unity on pressing play. I can't seem to find where the feedback loop is happening or what. After checking documentation for the functions i am using, everything should be tip-top.

please keep in mind, i have only been working with unity and C# for roughly 2 weeks, and have limited coding background before this. But i am a fast learner, and would Greatly appreciate any help fine tuning this code.
Lastly, had some issues formatting this post properly, not sure if the new line function works proper etc... so apologies for the kind of mess this is.

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by CheezyTatoSoup · Feb 09, 2021 at 08:25 PM

I figured it out... I was checking the list AFTER i put the item in, so OF COURSE it will find the item in the list... so it was going "Yes, it is there, repeat. Yes it is there, repeat" etc etc etc.....
I simply added a string 'check' in place of the "current combined Coords" variable. now it works, with no repeats.

wow... but YAY i got it. Thanks Unity Forums!

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

158 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 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 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 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 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 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

Spawn unique objects from the array 1 Answer

compare string to string array[] 2 Answers

System.Array.IndexOf iterating when it should not 1 Answer

Null Reference Exception when reading from array of strings 0 Answers

Array cannot read Greek characters 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