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
0
Question by applesandaliens73 · May 28, 2018 at 05:45 AM · instantiatestackoverflow

Instantiating prefab at random and unoccupied locations

So apparently taking a year off meant losing the little bit of programming knowledge I gained in college. I'm trying to make it so that my prefab cubes (called snooters because I'm a dweeb) are spawned at random locations, but don't ever occupy the same spot. So far it works well for the first ~10 or so, but occasionally I'll get either a "stack overflow exception" error or multiple cubes pop up at once.

     public GameObject snootInstance;
     public List <string> snootsInGame = new List<string>();
 
     public void chooseLocation()
     {
         //choose x spot
         int xSpot = Random.Range(-8, 8);
         //choose y spot
         int ySpot = Random.Range(-2, 4);
         //make them checkable coordinates
         string spotLocation = xSpot + ", " + ySpot;
         //check those coordinates
         decideToSpawn(spotLocation, xSpot, ySpot);
 
     }
 
     void decideToSpawn(string spotToCheck, int xSpot, int ySpot)
     {
         if (snootsInGame.Contains(spotToCheck) == true) //if the list has the coordinates already, go back and get new ones
         {
             chooseLocation();
         }
         else //if not, then go ahead to spawning
         {
             spawnNew(spotToCheck, xSpot, ySpot);
         }
         
     }
 
     void spawnNew(string spotToAdd, int xLocation, int yLocation)
     {
         snootsInGame.Add(spotToAdd); //add the coordinates to the list
         Instantiate(snootInstance, new Vector3(xLocation, yLocation, 0), Quaternion.identity); //make the cube
         print("spawned at " + xLocation + ", " + yLocation); //tell console where it spawned
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 Captain_Pineapple · May 28, 2018 at 08:25 AM

Hey there,

well since you wrote code that can run in a circle there is one case that you get a stack overflow. That is when you hit an already occupied Spot again and again until the stack is full.

Even though recursive functions can be nice sometimes i'd advice against it in this instance and propose the following:

Implement a 2D-Array with all the possible "Spots". Fill it with some markers like "0" for free and "1" for occupied. Or make it boolean, or a custom type. You can be creative here. Then instead of choosing a random position you get yourself a random number, let's call it P, between 0 and your (maxGridSizeX * maxGridSizeY - snootsInGame.Count) Then you iterate your 2D-Array until you reach the Pth empty position. This is now your random position to set your new Snoot to. This will eliminate the possibility of getting stuck in an endless loop.

Hope this helps. In case you need some coding help let me know.

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 applesandaliens73 · May 28, 2018 at 06:58 PM 0
Share

Right! That is a much more elegant solution. I remembered my professors telling my not to have functions circle like that but couldn't come up with an alternative; I completely forgot I could just build a grid. Thanks so much!

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

104 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

Related Questions

Checking if object intersects? 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

Setting the parent of a transform which resides in a prefab error 1 Answer

InvokeRepeating for random ball spawn 1 Answer

I need help with rotation 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