Question by
LoathsomeSk · Jun 22, 2021 at 11:48 AM ·
c#scripting problemfreezing
Unity freezes after I made a new script when i try to run game,
Trying to make a procedurally generated asteroid field anyone know why this freezes unity up completely to the point i have to end task when i run game? This is attached to an empty object I called GenerationCore...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerationCore : MonoBehaviour
{
public GameObject asteroid1Prefab;
public GameObject asteroid2Prefab;
public GameObject asteroid3Prefab;
public Vector3 solarSystemRange;
void Start()
{
GenerateSolarSystem();
}
void Update()
{
}
public void GenerateSolarSystem()
{
int amountOfAsteroidBelts = (Random.Range(1, 10));
Debug.Log("Asteroid Belt Generation Roll: " + amountOfAsteroidBelts);
if (amountOfAsteroidBelts > 0)
{
GenerateAsteroidBelts(amountOfAsteroidBelts);
}
}
public void GenerateAsteroidBelts(float _amountofAsteroidBelts)
{
GameObject newAsteroidBelt;
Vector3 newAsteroidBeltLocation;
float asteroidBeltDensity;
Vector3 asteroidBeltRange;
float placeAsteroidChance;
float placeWhichAsteroid;
Vector3 asteroidPlacementPosition;
float placedAsteroids = 0;
GameObject asteroidToInstantiate = asteroid1Prefab;
int avoidInfiniteLoop = 1;
for(int generatingAsteroidBelt = 1; generatingAsteroidBelt < _amountofAsteroidBelts; generatingAsteroidBelt++)
{
newAsteroidBeltLocation.x = Random.Range(0, solarSystemRange.x);
newAsteroidBeltLocation.y = Random.Range(0, solarSystemRange.y);
newAsteroidBeltLocation.z = Random.Range(0, solarSystemRange.z);
newAsteroidBelt = new GameObject("Asteroid Belt");
newAsteroidBelt.transform.position = newAsteroidBeltLocation;
asteroidBeltDensity = Random.Range(50, 2500);
asteroidBeltRange.x = asteroidBeltDensity * 100;
asteroidBeltRange.y = asteroidBeltDensity * 100;
asteroidBeltRange.z = asteroidBeltDensity * 100;
Debug.Log("Asteroid Belt Generated! Placing asteroids...");
//while (placedAsteroids < asteroidBeltDensity && avoidInfiniteLoop < 1000)
//{
for (int PositionX = 0; PositionX < asteroidBeltRange.x; PositionX += 10)
{
for (int PositionY = 0; PositionY < asteroidBeltRange.y; PositionY += 10)
{
for (int PositionZ = 0; PositionZ < asteroidBeltRange.z; PositionZ += 10)
{
if (placedAsteroids < asteroidBeltDensity)
{
placeAsteroidChance = Random.Range(1, 10000);
if (placeAsteroidChance > 9998)
{
// determine which asteroid to place
placeWhichAsteroid = Random.Range(1, 3);
if (placeWhichAsteroid == 1) asteroidToInstantiate = asteroid1Prefab;
if (placeWhichAsteroid == 2) asteroidToInstantiate = asteroid2Prefab;
if (placeWhichAsteroid == 3) asteroidToInstantiate = asteroid3Prefab;
// get location
asteroidPlacementPosition.x = PositionX;
asteroidPlacementPosition.y = PositionY;
asteroidPlacementPosition.z = PositionZ;
// place asteroid
GameObject newAsteroid = Instantiate(asteroidToInstantiate, (newAsteroidBelt.transform.position + asteroidPlacementPosition), Quaternion.identity) as GameObject;
newAsteroid.transform.parent = newAsteroidBelt.transform;
placedAsteroids = placedAsteroids + 1;
newAsteroid.name = "Asteroid #" + placedAsteroids;
}
}
}
}
}
avoidInfiniteLoop += 1;
// }
Debug.Log("Asteroids Generated! Total: " + placedAsteroids + ", Passes: " + avoidInfiniteLoop);
//exit while
}
}
}
,Anyone know why this code is freezing my unity program when I start the game? I took out the while loop and it still does it, not the best at programming but trying to make procedural generation...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerationCore : MonoBehaviour
{
public GameObject asteroid1Prefab;
public GameObject asteroid2Prefab;
public GameObject asteroid3Prefab;
public Vector3 solarSystemRange;
void Start()
{
GenerateSolarSystem();
}
void Update()
{
}
public void GenerateSolarSystem()
{
int amountOfAsteroidBelts = (Random.Range(1, 10));
Debug.Log("Asteroid Belt Generation Roll: " + amountOfAsteroidBelts);
if (amountOfAsteroidBelts > 0)
{
GenerateAsteroidBelts(amountOfAsteroidBelts);
}
}
public void GenerateAsteroidBelts(float _amountofAsteroidBelts)
{
GameObject newAsteroidBelt;
Vector3 newAsteroidBeltLocation;
float asteroidBeltDensity;
Vector3 asteroidBeltRange;
float placeAsteroidChance;
float placeWhichAsteroid;
Vector3 asteroidPlacementPosition;
float placedAsteroids = 0;
GameObject asteroidToInstantiate = asteroid1Prefab;
int avoidInfiniteLoop = 1;
for(int generatingAsteroidBelt = 1; generatingAsteroidBelt < _amountofAsteroidBelts; generatingAsteroidBelt++)
{
newAsteroidBeltLocation.x = Random.Range(0, solarSystemRange.x);
newAsteroidBeltLocation.y = Random.Range(0, solarSystemRange.y);
newAsteroidBeltLocation.z = Random.Range(0, solarSystemRange.z);
newAsteroidBelt = new GameObject("Asteroid Belt");
newAsteroidBelt.transform.position = newAsteroidBeltLocation;
asteroidBeltDensity = Random.Range(50, 2500);
asteroidBeltRange.x = asteroidBeltDensity * 100;
asteroidBeltRange.y = asteroidBeltDensity * 100;
asteroidBeltRange.z = asteroidBeltDensity * 100;
Debug.Log("Asteroid Belt Generated! Placing asteroids...");
//while (placedAsteroids < asteroidBeltDensity && avoidInfiniteLoop < 1000)
//{
for (int PositionX = 0; PositionX < asteroidBeltRange.x; PositionX += 10)
{
for (int PositionY = 0; PositionY < asteroidBeltRange.y; PositionY += 10)
{
for (int PositionZ = 0; PositionZ < asteroidBeltRange.z; PositionZ += 10)
{
if (placedAsteroids < asteroidBeltDensity)
{
placeAsteroidChance = Random.Range(1, 10000);
if (placeAsteroidChance > 9998)
{
// determine which asteroid to place
placeWhichAsteroid = Random.Range(1, 3);
if (placeWhichAsteroid == 1) asteroidToInstantiate = asteroid1Prefab;
if (placeWhichAsteroid == 2) asteroidToInstantiate = asteroid2Prefab;
if (placeWhichAsteroid == 3) asteroidToInstantiate = asteroid3Prefab;
// get location
asteroidPlacementPosition.x = PositionX;
asteroidPlacementPosition.y = PositionY;
asteroidPlacementPosition.z = PositionZ;
// place asteroid
GameObject newAsteroid = Instantiate(asteroidToInstantiate, (newAsteroidBelt.transform.position + asteroidPlacementPosition), Quaternion.identity) as GameObject;
newAsteroid.transform.parent = newAsteroidBelt.transform;
placedAsteroids = placedAsteroids + 1;
newAsteroid.name = "Asteroid #" + placedAsteroids;
}
}
}
}
}
avoidInfiniteLoop += 1;
// }
Debug.Log("Asteroids Generated! Total: " + placedAsteroids + ", Passes: " + avoidInfiniteLoop);
//exit while
}
}
}
Comment