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 W1k3 · Oct 20, 2014 at 02:50 AM · c#crash

Unity crashes when part of script is run (C#)

I apologize for having so little information and such a large script, but there is no error on a specified line that unity gives me. It just crashes.

I made a script. Of course it didn't work on the first try, so While going through fixing the problems, I have encountered 2 places where if the line is run, unity crashes. One is just an instantiation of an object, but I can't work around the other one.

The instantiation is on line 306, and the real problem happens on line 60, and/or 64 whenever "initialisations < xcue.Length" is include in the if statement. I can put the statement separately or before, but the result is the same.

Any idea whatsoever would be fantastic.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class floorbuilderv2 : MonoBehaviour {
 
     //room related
     public GameObject[] fourrooms = new GameObject[3];
     public GameObject[] threerooms = new GameObject[3];
     public GameObject[] tworooms = new GameObject[3];
     public GameObject[] onerooms = new GameObject[3];
     public GameObject[] startrooms = new GameObject[1];
 
     //settings
     public GameObject TestRepresent;
     public float distance = 10f;
 
     //flooor related
     int currentfloor = 0;
     public int Size_Increase_Factor = 5;
     List<placeholder[,]> existanceplaceholders = new List<placeholder[,]>();
     int roomcount = 0;
     int initialisations = 0; 
  
     //placeholder[][] existance = new placeholder[10][10]; 
 
     //misc
     void Start () {
         transform.position = new Vector3 (0, 0, 0);
         buildnextfloor ();
     }
     
     void buildnextfloor(){
         int gridsize = (currentfloor+1) * 20;
         int starttile = Mathf.FloorToInt (gridsize/2);
         int roomquantity = ((currentfloor+1) + (currentfloor+1)) * Size_Increase_Factor;
         int priorityfactor = 1;
         roomcount = 0;
         initialisations = 0;
 
         existanceplaceholders.Add(new placeholder[gridsize,gridsize]); 
 
         for (int i=0; i < (gridsize); i++) {
             for(int j =0; j < gridsize; j++){
                 existanceplaceholders[currentfloor][i, j] = new placeholder();
             }
         }
 
         ////////////////////////////////////////////////////////////////////////////////
 
         creattile (starttile, starttile, 4, priorityfactor, gridsize);
         priorityfactor ++;
 
         while(true){
             int[] xcue = findnextcue(gridsize, true);
             int[] ycue = findnextcue(gridsize, false);
 
             for(int i =0; i < initialisations; i++){
 
                 if(initialisations < xcue.Length){
                     break;
                 }
 
                 if(roomcount < roomquantity && initialisations < xcue.Length){
                     Debug.Log("Creating new tile (" + xcue[i] + ", " + ycue[i] + ")");
                     creattile(xcue[i], ycue[i], 0, priorityfactor, gridsize);
                 }
                 else{
                     break;
                 }
             }
             if(roomcount >= roomquantity || initialisations < xcue.Length){
                 break;
             }
         }
         buildnextfloorfaze2(gridsize);
     }
 
     void creattile(int x, int y, int staticdoorcount, int priority, int gridsize){
         existanceplaceholders[currentfloor][x, y].exists = true; 
         existanceplaceholders[currentfloor][x, y].initialised = true;
         existanceplaceholders [currentfloor] [x, y].location = generatecoordinates (x, y, gridsize); 
         Debug.Log("initialised room. Grid location: " + existanceplaceholders [currentfloor] [x, y].location);
 
         roomcount ++;
         existanceplaceholders [currentfloor] [x, y].priority = priority;
         int adjacents = 0;
 
         if (staticdoorcount > 0){
             if(staticdoorcount == 4){
                 existanceplaceholders[currentfloor][x+1, y].exists = true;
                 existanceplaceholders[currentfloor][x, y+1].exists = true;
                 existanceplaceholders[currentfloor][x-1 ,y].exists = true;
                 existanceplaceholders[currentfloor][x, y-1].exists = true;
                 initialisations += 4;
             }
         }
 
         else if ((int)Random.Range(1, 3) == 1){
             adjacents = 1;
         }
         else if((int)Random.Range(1, 3) != 1){
             adjacents = 2;
         }
         else{
             adjacents = 3;
         }
 
         int offsetx = 0;
         int offsety = 0;
 
         if(adjacents == 1){
             int attempts = 0;
 
             offsetx = setoffsetx();
             offsety = setoffsety(offsetx);
 
             while(existanceplaceholders[currentfloor][x +offsetx, y+offsety].exists == true){
                 offsetx = 0;
                 offsety = 0;
 
                 offsetx = setoffsetx();
                 offsety = setoffsety(offsetx);
 
                 attempts ++;
 
                 if(attempts > 3){
                     break;
                 }
             }
             if(attempts < 3){
                 existanceplaceholders[currentfloor][x +offsetx, y + offsety].exists = true;
                 initialisations ++;
             }
         }
         else if(adjacents == 2){
             for(int i=0; i<2; i++){
                 int attempts = 0;
                 
                 offsetx = setoffsetx();
                 offsety = setoffsety(offsetx);
                 
                 while(existanceplaceholders[currentfloor][x +offsetx, y+offsety].exists == true){
                     offsetx = 0;
                     offsety = 0;
                     
                     offsetx = setoffsetx();
                     offsety = setoffsety(offsetx);
                     
                     attempts ++;
                     
                     if(attempts > 3){
                         break;
                     }
                 }
                 
                 if(attempts < 3){
                     existanceplaceholders[currentfloor][x +offsetx, y + offsety].exists = true;
                     initialisations ++;
                 }
             }
         }
         else if(adjacents == 3){
             for(int i=0; i<3; i++){
                 int attempts = 0;
                 
                 offsetx = setoffsetx();
                 offsety = setoffsety(offsetx);
                 
                 while(existanceplaceholders[currentfloor][x +offsetx, y+offsety].exists == true){
                     offsetx = 0;
                     offsety = 0;
                     
                     offsetx = setoffsetx();
                     offsety = setoffsety(offsetx);
                     
                     attempts ++;
                     
                     if(attempts > 3){
                         break;
                     }
                 }
                 
                 if(attempts < 3){
                     existanceplaceholders[currentfloor][x +offsetx, y + offsety].exists = true;
                     initialisations ++;
                 }
             }
         }
     }
 
 
 
     int setoffsetx(){
         if(Random.Range(1,2) == 1){
             int rnd = Random.Range (1, 2);
                 
             if(rnd == 1){
                 return -1;
             }
             if (rnd == 2) {
                 return 1;
             }
         }
         else{
             return 0;
         }
         return 0;
     }
     int setoffsety(int offsetx){
         if(offsetx == 0){
             int rnd = Random.Range (1, 2);
             
             if(rnd == 1){
                 return -1;
             }
             if (rnd == 2) {
                 return 1;
             }
         }
         else{
             return 0;
         }
         return 1;
     }
 
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
     Vector3 generatecoordinates(int x, int y, int gridsize){
         int starttile = Mathf.FloorToInt (gridsize/2);
         return new Vector3 (((starttile - x)*distance),(0f),((starttile-y)*distance));
     }
 
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
     int[] findnextcue(int gridsize, bool findx){
 
         int[] cue = new int[initialisations];
         int tempi = 0;
 
         if(findx){
             for(int i = 0; i < gridsize; i++){
                 for(int ii = 0; ii < gridsize; ii++){
                     if(existanceplaceholders[currentfloor][i,ii].exists == true && existanceplaceholders[currentfloor][i,ii].initialised == false){
                         cue[tempi] = i;
                         tempi ++;
                     }
                 }
             }
             return cue;
         }
         else{
             for(int i = 0; i < gridsize; i++){
                 for(int ii = 0; ii < gridsize; ii++){
                     if(existanceplaceholders[currentfloor][i,ii].exists == true && existanceplaceholders[currentfloor][i,ii].initialised == false){
                         cue[tempi] = ii;
                         tempi ++;
                     }
                 }
             }
             return cue;
         }
         return null;
     }
 
     ////////////////////////////////////////////////////////////////////////////////////
 
     int[] initialisedcue(int gridsize, bool findx){
         
         int[] cue = new int[initialisations];
         int tempi = 0;
         
         if(findx){
             for(int i = 0; i < gridsize; i++){
                 for(int ii = 0; ii < gridsize; ii++){
                     if(existanceplaceholders[currentfloor][i,ii].initialised == true){
                         cue[tempi] = i;
                         tempi ++;
                     }
                 }
             }
             return cue;
         }
         else{
             for(int i = 0; i < gridsize; i++){
                 for(int ii = 0; ii < gridsize; ii++){
                     if(existanceplaceholders[currentfloor][i,ii].initialised == true){
                         cue[tempi] = ii;
                         tempi ++;
                     }
                 }
             }
             return cue;
         }
         return null;
     }
 
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
     void buildnextfloorfaze2(int gridsize){
 
         int[] xcue = initialisedcue(gridsize, true);
         int[] ycue = initialisedcue(gridsize, false);
 
         for(int i = 0; i < xcue.Length; i++) {
             //Instantiate(TestRepresent, existanceplaceholders[currentfloor][xcue[i], ycue[i]].location, Quaternion.identity);
         }
         Debug.Log("Finished instantiation");
 
     }
 
     //void Update () {
     //}
 }
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
2

Answer by mattyman174 · Oct 20, 2014 at 03:14 AM

 while(true){

Without a doubt this infinite loop is the source of your problem. In my books infinite loops are bad practice especially if not implemented correctly.

I would advise you go back and rework the code lines 54 - 75 where your infinite loop is and find a solution that doesn't need an ambiguous infinite loop.

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 Linus · Oct 20, 2014 at 04:36 AM 0
Share

Agree, seems like i < initialisations never comes true, so it never breaks. initialisations is set to 0 line 39

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

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

App crashes after splash screen with Google Play Services for AR update (after April 11th 2020) 2 Answers

This jump code crashes the editor! 1 Answer

Why does Unity crash? (and other programs in general) 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