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 /
avatar image
1
Question by era · Jan 24, 2010 at 02:35 PM · instantiaterandom

Mystery: Unexpected behavior with randoms and arrays

I'm working with an array of arrays to fill a grid (with actual size CurrentGridSize) with objects. I use randoms to determine the position the objects should take in the array and thus on the grid. The current expected behavior is that there will be around (CurrentGridSize x 4) objects in the array (barring doubles). However, the actual behavior is: the amount of objects is always exactly (CurrentGridSize). And I just can't figure out why that is. Even if I execute the loop 1000 times, the behavior stays the same. Can someone help me out and point out where the problem might be?

Much thanks to whoever can solve this mystery!

for(var k=0;k<(CurrentGridSize*4);++k) {

     //calculate randoms for placement on grid
     HorIndex = (parseInt(Random.Range(0,CurrentGridSize)));
     VertIndex = (parseInt(Random.Range(0,CurrentGridSize)));


         //if statement to prevent doubles
     if(CompleteGrid[HorIndex][VertIndex] == 0)
     {
         //instantiate and move to correct position
         CompleteGrid[HorIndex][VertIndex] = Instantiate(IslandType);
         CompleteGrid[HorIndex][VertIndex].transform.position.x = HorIndex*SquareSize;
         CompleteGrid[HorIndex][VertIndex].transform.position.z = VertIndex*SquareSize;    
     }
 }

EDIT I've discovered where the problem might be: if I leave out the if() statement to check if the element is zero, it does work correctly. But now I still don't know why this is the case...

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ashkan_gc · Jan 24, 2010 at 03:28 PM

the problem is when your new random number is a full array element, you should subtract k by one to run another iteration to find an empty element. 1/4 of random numbers are full elements in your current code so the object will not be instantiated. also you can define a label to go back to it with goto statement when the array element is not equal to zero.

for(var k=0;k<(CurrentGridSize*4);++k) {

     //calculate randoms for placement on grid
     HorIndex = (parseInt(Random.Range(0,CurrentGridSize)));
     VertIndex = (parseInt(Random.Range(0,CurrentGridSize)));


     //if statement to prevent doubles
     if(CompleteGrid[HorIndex][VertIndex] == 0)
     {
     //instantiate and move to correct position
             CompleteGrid[HorIndex][VertIndex] = Instantiate(IslandType);
             CompleteGrid[HorIndex][VertIndex].transform.position.x = HorIndex*SquareSize;
             CompleteGrid[HorIndex][VertIndex].transform.position.z = VertIndex*SquareSize;      
     }
     else
     {
               k--;
     }

}

Comment
Add comment · Show 2 · 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 era · Jan 24, 2010 at 03:43 PM 0
Share

Sorry, that's not it either. The point of the CurrentGridSize is to not exceed that size! It's an array of arrays: the first deter$$anonymous$$es horizontal postion, the second vertical. That being said, there are actually CurrentGridSize available positions in the array. if I let the random exceed this by saying (CurrentGridSize*4) using the randoms, that still doesn't change the weird behavior and it defeats the whole point of having a set size for the grid.

avatar image Ashkan_gc · Jan 24, 2010 at 06:40 PM 0
Share

i edited the question. i think i found the answer. the problem is you skip the iteration that could not instantiate a new object in an empty element.

avatar image
0

Answer by Lucas Meijer 1 · Jan 24, 2010 at 03:01 PM

It looks like you actually create 4xCurrentGridSize objects, but it looks quitely likely that you move some of them to exactly the same position, making it look like there's only one. Check your hierarchy for the actual amount of islands created.

Comment
Add comment · Show 3 · 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 era · Jan 24, 2010 at 03:19 PM 0
Share

alas, I already thought of that. The code actually creates CurrentGridSize objects, that's how many there are present. Creating another one at the same position is also prevented in the code (all the elements in the array are initialized at 0, if an element isn't zero, it's taken and can't be taken again).

avatar image duck ♦♦ · Feb 17, 2010 at 12:30 PM 0
Share

How exactly are you deter$$anonymous$$ing the number of objects created? (just to make sure there's no error there!)

avatar image era · Mar 19, 2010 at 09:01 AM 0
Share

I checked the Hierarchy panel and the Scene to cross reference and make sure. Anyway, I still haven't found the solution to this riddle but I've written a workaround that does the trick just as well. I still hope to figure it out one day and I'll let you know!

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

No one has followed this question yet.

Related Questions

Instantiating a random dropped consumable item from many cloned objects 1 Answer

How to instantiate 2D object at fixed location? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Spawning objects? 1 Answer

Instantiate multiple objects and have them raycast 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