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 MadmanSilver · Feb 25, 2015 at 08:10 PM · crashproceduralgenerationtiles

Tile-based Generation Not Working?

Ok, so I was testing some things for a project I am working on, and ran into a problem. I have created a script where, two seconds after the game starts, it will generate a map of basic little squares that I have stored as a prefab. Whenever I run the script, the game crashes. That's it, no errors, not even a single tile spawns. I have looked over it several times, but could not figure it out. Yes, it is a fairly primitive and simple way of handling this type of generation, but that is sort of the point. All help is appreciated.

Here is the script that actually does the generation:

 public class Generator : MonoBehaviour {
     
     public GameObject tile;
     public bool done = false;
     public int size = 10;
 
     private GridCords origin = new GridCords(0, 0);
     private GridCords current;
     private GridCords last;
     private List<GridCords> hist = new List<GridCords>();
     private GridCords[] choices = new GridCords[4];
     private GameObject newTile;
     private bool first = true;
     private int past = 0;
 
     // Initialize stuff.
     public void Start() {
         current = origin;
         last = origin;
         choices[0] = new GridCords(last.GetX() + 1, last.GetY());
         choices[1] = new GridCords(last.GetX() - 1, last.GetY());
         choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
         choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
     }
     
     public void Generate() {
         if (size < 1) {
             done = true;
             return;
         }
 
         if (first) {
             newTile = (GameObject)Instantiate(tile, origin.ToVect(), tile.transform.rotation);
             hist.Add(origin);
             last = origin;
         } else {
             // Wait a bit.
             Wait(2.0f);
             
             // Prepare for generation.
             choices[0] = new GridCords(last.GetX() + 1, last.GetY());
             choices[1] = new GridCords(last.GetX() - 1, last.GetY());
             choices[2] = new GridCords(last.GetX(), last.GetY() + 1);
             choices[3] = new GridCords(last.GetX(), last.GetY() - 1);
             
             // Generate.
             foreach (GridCords gc in choices) {
                 if (hist.Contains(gc)) {
                     past++;
                 }
             }
 
             if (past > 3) {
                 done = true;
                 return;
             }
 
             int i = 0;
 
             while (hist.Contains(current)) {
                 current = choices[0];
                 i++;
             }
 
             newTile = (GameObject)Instantiate(tile, current.ToVect(), tile.transform.rotation);
             
             // Post-generation.
             hist.Add(current);
             last = current;
             size--;
             past = 0;
         }
     }
 
     private IEnumerator Wait(float secs) {
         yield return new WaitForSeconds(secs);
     }
 }

Here is the GridCords class, which has the sole purpose of making my code a little more manageable:

 public class GridCords : MonoBehaviour {
 
     private int x, y;
 
     public GridCords(int x, int y) {
         this.x = x;
         this.y = y;
     }
 
     // Creates world co-ordinates using the grid co-ordinates specified.
     public Vector3 ToVect() {
         return new Vector3(y * 10, 0, x * 10);
     }
 
     public int GetX() {
         return x;
     }
 
     public int GetY() {
         return y;
     }
 }

This should be all you need to figure it out. This is a completely separate project file, so the other things should have no effect on this.

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 maccabbe · Feb 25, 2015 at 08:37 PM

You never call Generate from either of these scripts so I can't tell exactly what is broken in your script but my guess it that you are calling Generate in a third script until size goes to 0. However since first is never set to false after creating a tile, size never goes down and the program is stuck in an infinite loop. Try using the following Generator :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Generator : MonoBehaviour {
 
     public GameObject tile;
     public bool done=false;
     public int size=10;
 
     private GridCords origin=new GridCords(0, 0);
     private GridCords current;
     private GridCords last;
     private List<GridCords> hist=new List<GridCords>();
     private GridCords[] choices=new GridCords[4];
     private GameObject newTile;
     private bool first=true;
     private int past=0;
 
     // Initialize stuff.
     public void Start() {
         current=origin;
         last=origin;
         choices[0]=new GridCords(last.GetX()+1, last.GetY());
         choices[1]=new GridCords(last.GetX()-1, last.GetY());
         choices[2]=new GridCords(last.GetX(), last.GetY()+1);
         choices[3]=new GridCords(last.GetX(), last.GetY()-1);
     }
 
     float time=0;
     const float delay=0.5f;
     public void Update() {
         time=time+Time.deltaTime;
         while(time>delay) {
             Generate();
             time=time-delay;
         }
     }
 
     public void Generate() {
         if(size<1) {
             done=true;
             return;
         }
 
         if(first) {
             newTile=(GameObject)Instantiate(tile, origin.ToVect(), tile.transform.rotation);
             hist.Add(origin);
             last=origin;
             size--;
             first=false;
         }
         else {
             // Prepare for generation.
             choices[0]=new GridCords(last.GetX()+1, last.GetY());
             choices[1]=new GridCords(last.GetX()-1, last.GetY());
             choices[2]=new GridCords(last.GetX(), last.GetY()+1);
             choices[3]=new GridCords(last.GetX(), last.GetY()-1);
 
             // Generate.
             foreach(GridCords gc in choices) {
                 if(hist.Contains(gc)) {
                     past++;
                 }
             }
 
             if(past>3) {
                 done=true;
                 return;
             }
 
             int i=0;
 
             while(hist.Contains(current)) {
                 current=choices[0];
                 i++;
             }
 
             newTile=(GameObject)Instantiate(tile, current.ToVect(), tile.transform.rotation);
 
             // Post-generation.
             hist.Add(current);
             last=current;
             size--;
             past=0;
         }
     }
 }

Also GridCoords should not inherit from Monobehaviour. It does not override any methods from monobehaviour and is never attached to a gameObject.

 using UnityEngine;
 using System.Collections;
 
 public class GridCords  {
 
     private int x, y;
 
     public GridCords(int x, int y) {
         this.x=x;
         this.y=y;
     }
 
     // Creates world co-ordinates using the grid co-ordinates specified.
     public Vector3 ToVect() {
         return new Vector3(y*10, 0, x*10);
     }
 
     public int GetX() {
         return x;
     }
 
     public int GetY() {
         return y;
     }
 }
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 MadmanSilver · Feb 25, 2015 at 10:11 PM 0
Share

Oh, yeah. Thanks, I didn't actually set first to false... Thanks... again!

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

2 People are following this question.

avatar image avatar image

Related Questions

Overlapping Tile Generation 0 Answers

Procedural generation of dungeons with rooms made in the new tile map system? 0 Answers

Procedurally Generated Room System... 1 Answer

How would I procedarally generate a terrain? 2 Answers

2D Procedural Terrain Generation + Pooling 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