- Home /
Unity crashes after I change 1 symbol in code!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlorScript : MonoBehaviour
{
public int size, sizeleft;
public int[] sizeX;
public int a,b,c;
public GameObject[] objts;
// public GameObject lastspwnd;
public GameObject pos1;
public bool isSpace;
private void Start()
{
sizeleft = size;
a = Random.Range(0, 3);
Instantiate(objts[a], pos1.transform.position, pos1.transform.rotation);
sizeleft -= sizeX[a];
pos1.transform.position -= new Vector3(-3f, 0, 0);
isSpace = false;
Log(a," <-- First random");
for(int i = 0; i <= 10; i++) //How many objects we can spawn;
{
for(int k = 0; k <= 3; k++) //Check if there are object that can fill empty space;
{
isSpace = false; // isSpace set to false to avoid issues;
if(sizeleft >= sizeX[k]) // If Yes than stop check;
{
isSpace = true;
Log(k, " break");
break;
}
}
if (!isSpace) // If ther is not empty space than STOP
{
break;
}
if (isSpace) // Start searching desired object;
{
while (isSpace) //repeat cheking until the desired object is found;
{
a = Random.Range(0, 3);
**if (sizeleft >= size[a])
{
Instantiate(objts[a], pos1.transform.position, pos1.transform.rotation);
sizeleft -= sizeX[a];
isSpace = false;
break;
}**
}
}
}
}
public void Log(int value, string massage)
{
Debug.Log(value + " || " + massage);
}
}
When I change ">=" to "<=" in last if it work but on the other wrong way. But if I left everything without changes then Unity crashes and i need to restart it; What's wrong? Thanks) P.S. I'm not a native English speaker)
Answer by unity_ek98vnTRplGj8Q · May 27, 2020 at 02:51 PM
sizeLeft is probably never less than any of the values in the size array, and you are getting stuck in an infinite while loop because isSpace is never being set to false. Make sure that you either are guaranteed to exit the while loop or that you force it to exit after a few loops.
O$$anonymous$$G, I understand that one of the sizeX is zero and because of it first for loop says that isSpace is true after what while loop became infinity. THANKS!)))
Your answer
Follow this Question
Related Questions
Random crash in ntdll.dll 0 Answers
Game no reopen before go to Desktop or something (FullScreen) 0 Answers
Unity Editor Crashes Upon Startup 0 Answers
Unity crashing constantly 2 Answers