Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 figman123456 · Apr 30, 2014 at 04:02 PM · randomfreezegenerationhangrandom gen

Somthing about my script i making unity hang if i set the room count any higher that 0 can't figure out what though ?

Hi something about the script i have written to randomly generate a dungeon is making unit hang, and i can't think what is and cause of the hang can't debug it very well.

Here is the code: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class Genarator : MonoBehaviour {
     
 
     public List<GameObject> right = new List<GameObject>();
     public List<GameObject> left = new List<GameObject>();
     public List<GameObject> up = new List<GameObject>();
     public List<GameObject> down = new List<GameObject>();
 
     public int roomSize = 10;
     public int roomCount = 50;
 
     private int roomsMade = 0;
 
     private int[][] map;
 
     // Use this for initialization
     void Start () {
         startGeneration();
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     private void startGeneration()
     {
         GameObject first = right[0];
         Instantiate(first, Vector3.zero,Quaternion.identity);
         GetRoomsToGen(first);
 
     }
 
     private void GetRoomsToGen(GameObject room)
     {
 
         DirectionsAvailable temp = room.GetComponent<DirectionsAvailable>();
 
         if(temp.Right && roomsMade < roomCount)
         {
             GenarateRoomRight(room);
 
             roomsMade++;
         }
         if(temp.Left && roomsMade < roomCount)
         {
             GenarateRoomLeft(room);
             roomsMade++;
         }
         if(temp.Up && roomsMade < roomCount)
         {
             GenarateRoomUp(room);
             roomsMade++;
         }
         if(temp.Down && roomsMade < roomCount)
         {
             GenarateRoomDown(room);
             roomsMade++;
         }
     }
 
     private void GenarateRoomRight(GameObject previousRoom)
     {
         int rand = Random.Range(0,right.Count-1);
         GameObject tempRoom = right[rand];
         Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
         GetRoomsToGen(tempRoom);
     }
     private void GenarateRoomLeft(GameObject previousRoom)
     {
         int rand = Random.Range(0,left.Count-1);
         GameObject tempRoom = left[rand];
         Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
         GetRoomsToGen(tempRoom);
     }
     private void GenarateRoomUp(GameObject previousRoom)
     {
         int rand = Random.Range(0,up.Count-1);
         GameObject tempRoom = up[rand];
         Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
         GetRoomsToGen(tempRoom);
     }
     private void GenarateRoomDown(GameObject previousRoom)
     {
         int rand = Random.Range(0,down.Count-1);
         GameObject tempRoom = down[rand];
         Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
         GetRoomsToGen(tempRoom);
     }
 }

Any help would be greatly appreciated :) Thanks

Update: I have narrowed it down to the GenerateRoom methods and i was wondering it it was been caused by them all trying to get the rooms at once?

Thanks again

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 robertbu · Apr 30, 2014 at 09:25 PM

You have a recursive function, but you are not increasing 'roomsMade' in the right place. For example, start with the base case of roomCount is 1 and roomsMade = 0;

  • On Line 31 you call GetRoomsToGen()

  • On Line 42 you call GenarateRoomRight();

  • On Line 68 you again call GetRoomsToGen();

  • On Line 42 you again call GenerateRoomRight();

Unless there is something in temp.Right that would stop the cycle, you've created an infinite recursion. The program will hang, and then the stack will blow up. As a starting point for a fix, move the 'roomsMade++;' to just below the four Instantiate() calls so that the value is incremented before you next call GetRoomsToGen().

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 figman123456 · May 03, 2014 at 05:30 PM 0
Share

Thank you :) i don't know why i didn't notice it i think i just glazed over :S thank you again :D

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

20 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

Related Questions

How can I make a Terraria Style game from sprites without using an enormous amount of gameobjects? 2 Answers

Help with Generating Random Tiles 1 Answer

Random Button / Plane Generation. 0 Answers

How to generate random terrain for a platformer 0 Answers

Random number different from previous generated. 6 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