Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NotEnoughLemons3D · Oct 29, 2016 at 03:37 PM · c#arrayslistsmathf.clampindexoutofrangeexception

C# List index confusion using Mathf.Clamp

I have an object that is made up of say around 24 separate platforms that are connected in a 4X6 rectangle. I am attempting to make these platforms fall from the back to the front by setting isKinematic on their rigidbody from true to false. Before I continue explaining here is the part of the code that makes the platforms fall.

 IEnumerator FallPlatforms (){
         
     
 
         while (Platforms.Count > 0){
             int x = Random.Range (0, 5);
             Mathf.Clamp (x, 0, Platforms.Count-1);
             print ("X:" + x + " Platform Count:" + Platforms.Count);
             if (Platforms [x] == null) {
                 break;
             }
             Rigidbody rigi = Platforms [x].gameObject.GetComponent<Rigidbody> ();
             if (rigi != null) {
                 rigi.isKinematic = false;
             }
             Platforms.Remove (Platforms[x]);
 
             yield return new WaitForSeconds ((0.02f)*difficulty);
         }
     }

The falling is supposed to be somewhat random, but still from the back to the front. Therefore I choose a random index between 0 and 5, I use Mathf.Clamp to make sure the random index is within the range of the list Platforms, then I cause that platform to fall, then I remove it form the list. It works perfectly until there gets to be 2 platforms left. Unity returns a "ArgumentOutOfRangeException: Argument is out of range." in the Debug Log.

Mathf.Clamp and Random.Range are both inclusive according to Unity's documentation.

As you can see I placed print ("X:" + x + " Platform Count:" + Platforms.Count); to help try and figure out this problem. Right before Unity gives me the error, the print statement reads: "X:2 Platform Count:2". Yet, this should be impossible, because if Platforms.Count = 2, then my mathf.Clamp call should clamp x between 0 and 1 (effectively making x = 1). I am completely lost on how and why this is happening and any help would be greatly appreciated. Thanks!

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
1
Best Answer

Answer by doublemax · Oct 29, 2016 at 04:05 PM

You're not assining the return value of Mathf.Clamp to anything. So basically this line does nothing.

BTW: If you're passing integers, Random.Range is not inclusive. Passing 0 and 5 will return 4 at max. I'm not sure why you need the clamp anyway. Why don't you just pass 0 and Platforms.Count to Random.Range?

Comment
Add comment · Show 1 · 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 NotEnoughLemons3D · Nov 02, 2016 at 03:16 AM 0
Share

For some reason I was thinking mathf.clamp was one of those functions that modified the value itself. Not sure the technical term for it or how I missed that but thank you! Also, I can't pass Platforms.Count to Random.Range because then it would have a random fall through the entire 4x6 cells. I want the back cells to fall first, and in my list Platforms, it goes form back cells to the front. So it is somewhat random, yet still from back to the front for the most part. Thank you again and thanks for the Random.Range information.

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

232 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Why is my List adding to same array object again and again in update? 1 Answer

Endless Runner with new Biome each time 0 Answers

Increase List of Spawns Dynamically 1 Answer

All crops in list growing at once when they are supposed to grow individually 0 Answers

Trying to program two buttons to appear when the player in my game dies 0 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