Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
0
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
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

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

String to int conversion not working in ControllerCollider method although I am sure it is an int... 1 Answer

How do you spawn a gameobject based on last object in array and limit that within a range? 1 Answer

Trying to spawn enemies on only one path 1 Answer

Error FormatException: Input string was not in a correct format. 0 Answers

Random.Range returns either 1 or 2 2 Answers


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