- Home /
 
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 () {
     //}
 }
 
              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.
Agree, seems like i < initialisations never comes true, so it never breaks. initialisations is set to 0 line 39 
Your answer