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 TUNG_LP · Jul 11, 2016 at 07:33 PM · c#scriptingproblemfreezing

This Map Generator is freezing unity

MapGenerator script: Here is the script that I am learning from a unity game dev. named Sabatian Lague I follow all his tutorials as closely as possible but as I try to click on the map if freezes and this happens when I press play as well! No error I'm running unity under the quickest and lowest settings for the best performance, which still doesn't help the freezing, is there away i can fix this issue so i can continue the game development?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MapGenerator : MonoBehaviour {
 
     public Transform tilePrefab;
     public Transform obsticlePrefab;
     public Vector2 mapSize;
 
     [Range(0,1)]
     public float outlinePercent;
 
     [Range(0, 1)]
     public float obsticlePercent;
 
     List<Coord> allTileCoords;
     Queue<Coord> shuffledTileCoords;
 
     public int seed = 10;
     Coord mapCenter;
 
     void Start() {
         GenerateMap();
     }
 
 
     public void GenerateMap()
     {
 
         allTileCoords = new List<Coord>();
 
 
         for (int x = 0; x < mapSize.x; x ++)
         {
             for (int y = 0; y < mapSize.y; y ++)
             {
                 allTileCoords.Add(new Coord(x, y));
             }
         }
 
         shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), seed));
         mapCenter = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);
 
         string holderName = "GeneratedMap";
         if (transform.FindChild(holderName))
         {
             DestroyImmediate(transform.FindChild(holderName).gameObject);
         }
 
         Transform mapHolder = new GameObject(holderName).transform;
         mapHolder.parent = transform;
 
         for (int x = 0; x < mapSize.x; x++)
         {
             for (int y = 0; y < mapSize.y; y++)
             {
                 Vector3 tileposition = CoordToPosition(x, y);
                 Transform newTile = Instantiate(tilePrefab, tileposition, Quaternion.Euler(Vector3.right * 90)) as Transform;
                 newTile.localScale = Vector3.one * (1 - outlinePercent);
                 newTile.parent = mapHolder;
             }
         }
         bool[,] obsticleMap = new bool[(int)mapSize.x, (int)mapSize.y];
 
         int obsticleCount = (int)(mapSize.x * mapSize.y * obsticlePercent);
         int currentObsticleCount = 0;
         for (int i = 0; i < obsticleCount; i ++)
         {
             Coord randomCoord = GetRandomCoord();
             obsticleMap[randomCoord.x, randomCoord.y] = true;
             currentObsticleCount ++;
 
             if (randomCoord != mapCenter && MapIsFullyAccessible(obsticleMap,currentObsticleCount))
             {
                 Vector3 obsticlePosition = CoordToPosition(randomCoord.x, randomCoord.y);
 
                 Transform newObsticle = Instantiate(obsticlePrefab, obsticlePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
                 newObsticle.parent = mapHolder;
             }
             else
             {
                 obsticleMap[randomCoord.x, randomCoord.y] = false;
                 currentObsticleCount --;
             }
         }
     }
 
     bool MapIsFullyAccessible(bool[,] obsticleMap,int currentObsticleCount)
     {
         bool[,] mapFlags = new bool[obsticleMap.GetLength(0), obsticleMap.GetLength(1)];
         Queue<Coord> queue = new Queue<Coord>();
         queue.Enqueue(mapCenter);
         mapFlags[mapCenter.x, mapCenter.y] = true;
 
         int accessibleTileCount = 1;
 
         while (queue.Count > 0)
         {
             Coord tile = queue.Dequeue();
 
             for (int x = -1; x <= 1; x++)
             {
                 for (int y = -1; x <= 1; y++)
                 {
                     int neighborX = tile.x + x;
                     int neighborY = tile.y + y;
                     if (x==0 || y == 0)
                     {
                         if (neighborX >= 0 && neighborX < obsticleMap.GetLength(0) && neighborY >= 0 && neighborY < obsticleMap.GetLength(1)) 
                         {
                             if (!mapFlags[neighborX, neighborY] && !obsticleMap[neighborX, neighborY]) 
                             {
                                 mapFlags[neighborX, neighborY] = true;
                                 queue.Enqueue(new Coord(neighborX, neighborY));
 
                                 accessibleTileCount++;
                             }
                         }
                     }
                 }
             }
         }
 
         int targetAccessibleTileCount = (int)(mapSize.x * mapSize.y - currentObsticleCount);
         return targetAccessibleTileCount == accessibleTileCount;
     }
 
     Vector3 CoordToPosition(int x, int y)
     {
         return new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
     }
 
     public Coord GetRandomCoord()
     {
         Coord randomCoord = shuffledTileCoords.Dequeue();
         shuffledTileCoords.Enqueue(randomCoord);
         return randomCoord;
     }
 
     public struct Coord
     {
         public int x;
         public int y;
 
         public Coord(int _x, int _y)
         {
             x = _x;
             y = _y;
         }
 
         public static bool operator == (Coord c1, Coord c2)
         {
             return c1.x == c2.x && c1.y == c2.y;
         }
         public static bool operator !=(Coord c1, Coord c2)
         {
             return !(c1 == c2);
         }
     }
 }
 
 

Map Editor:

 using System.Collections;
 
 public static class Utility
 {
     public static T[]ShuffleArray<T>(T[]array, int seed)
     {
         System.Random prng = new System.Random(seed);
 
         for (int i=0; i < array.Length -1; i++)
         {
             int randomIndex = prng.Next(i, array.Length);
             T tempItem = array[randomIndex];
             array[randomIndex] = array[i];
             array[i] = tempItem;
         }
 
         return array;
     }
 }
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
3
Best Answer

Answer by Jessespike · Jul 11, 2016 at 08:42 PM

A lot of the time, a freeze means there is an infinite loop. You might want to change this:

 for (int y = -1; x <= 1; y++)
Comment
Add comment · Show 4 · 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 TUNG_LP · Jul 12, 2016 at 06:09 PM 0
Share

now if I change this would i put in an if statement in to say if map generator isnot generating then freeze this section of code to stop the infinite loop?

avatar image Eno-Khaon TUNG_LP · Jul 12, 2016 at 06:24 PM 1
Share

No, it's not that -- The conditional in the middle of the for loop is using the wrong variable. You're checking the state of x, but modifying y.

avatar image TUNG_LP · Jul 12, 2016 at 06:25 PM 0
Share

never $$anonymous$$d i relized when i C&Ped i never changed one of the x's to a y...

avatar image chomps32 · Jul 12, 2016 at 07:04 PM 1
Share

Lol.. so simple. Thumbs up

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Coroutine crashes game 2 Answers

The script is working but why when I try to change stuff in inspector the game is freezing ? 1 Answer

How to watch variables from another script efficiently? 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