Need Help. Unity freezes when I run my code

// this code is for generating planets on random locations and sizes on my 2d game without having them spawn in eachother.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlanetGenerator : MonoBehaviour {
    public GameObject startingplanet;
    public static PlanetGenerator global;
    public List<planet> planets = new List<planet> ();
    public List<GameObject> planetslist = new List<GameObject>();
    int numberofplanets;
    // Use this for initialization
    void Start () {
        PlanetGenerator.global = GetComponent<PlanetGenerator>();
        planetslist.Add (startingplanet);
                numberofplanets = Random.Range (1, 30);
                for (int i = 0; numberofplanets > i; i++) {
                    Debug.Log("new planet");
                    planet newplanet = new planet ("forest");
                    planets.Add(newplanet);

                }
        }
 
    // Update is called once per frame
    void Update () {
 
    }
}
public class planet{
    public GameObject planetprefab;
    Vector3 spawnlocation;
    private List<GameObject> planetlist;
    private int scale;

    public planet(string type){
        if(type == "forest"){
            planetlist = PlanetGenerator.global.planetslist;
            planetprefab = Resources.Load("Prefabs/Planet") as GameObject;
            int numberoftrees = Random.Range(5,20);
            int numberofrocks = Random.Range(3,15);


                for(int i = 0; 1 > i; i++){
                Debug.Log ("generating transform");
                scale = Random.Range(1,6);
                spawnlocation = new Vector3(Random.Range(-100,100),Random.Range(-100,100),0);
                for(int a = 0; planetlist.Count > a; a++){
                    GameObject theplanet = planetlist[a];
                    Vector2 planetlocation;
                    planetlocation.x = theplanet.transform.position.x;
                    planetlocation.y = theplanet.transform.position.y;
                    float theplanetradious =     theplanet.transform.GetComponent<CircleCollider2D>().radius;
                    float secondplanetradious = theplanet.transform.GetComponent<CircleCollider2D>().radius * scale;
                    if (Vector2.Distance(planetlocation, spawnlocation) > theplanetradious + secondplanetradious){
                            i = i - 1;
                        Debug.Log("wrong spawn");
                        }
                    }
                }


            Quaternion rotation = new Quaternion(0,0,0,0);
            GameObject newplanet = GameObject.Instantiate(planetprefab,spawnlocation,rotation) as GameObject;
            newplanet.transform.localScale = newplanet.transform.localScale * scale;
            PlanetGenerator.global.planetslist.Add(newplanet);
                Debug.Log ("spawned planet");



            //treespawner(numberoftrees);
            //rockspawner(numberofrocks);

        }
    }
}

Please format your post using CODE tags, they make it way more readable.

Done. Thanks.

I think you need to look up a “do…while” loop, you seem to be hacking something similar together with a for loop but if unity is freezing it’s obviously not working and getting stuck in the loop.

What debug lines are outputted?

i tried the while loop but it just ran the code in the code once even though the condition for the while loop to run was true. and none of the debug lines are outputted, not even the ones from other scripts

This:
for(int i =0; 1> i; i++){

is kind of weird. That loop newer run more that once. Its, not that would cause the freezing, but still probably something that wasn’t your purpose.

edit: oh i see, you are manipulating i later:
i = i -1;
perhaps there is logic error that causing freeze?

there’s a i-- line further down is it doesn’t find what its after… hence the do…while suggestion :face_with_spiral_eyes:

yeah …just saw that and edited my post

i just rewrote my code to try the while loop like this:

public planet(string type){
        if(type == "forest"){
            planetlist = PlanetGenerator.global.planetslist;
            planetprefab = Resources.Load("Prefabs/Planet") as GameObject;
            int numberoftrees = Random.Range(5,20);
            int numberofrocks = Random.Range(3,15);
            bool spawnedright = false;

                while ( spawnedright == false ){
                Debug.Log ("generating transform");
                scale = Random.Range(1,6);
                spawnlocation = new Vector3(Random.Range(-100,100),Random.Range(-100,100),0);
                bool spawning = true;
                for(int a = 0; PlanetGenerator.global.planetslist.Count > a; a++){
                    GameObject theplanet = PlanetGenerator.global.planetslist[a];
                    Vector2 planetlocation;
                    planetlocation.x = theplanet.transform.position.x;
                    planetlocation.y = theplanet.transform.position.y;
                    float theplanetradious =     theplanet.transform.GetChild (0).GetComponent<CircleCollider2D>().radius;
                    float secondplanetradious = theplanet.transform.GetChild (0).GetComponent<CircleCollider2D>().radius * scale;
                    if (Vector2.Distance(planetlocation, spawnlocation) < theplanetradious + secondplanetradious){
                        spawning = false;
                        Debug.Log("false spawn");
                        }
                    }
                    if(spawning == true){
                        spawnedright = true;
                    }
                }

            if (spawnedright == true){
            Quaternion rotation = new Quaternion(0,0,0,0);
            GameObject newplanet = GameObject.Instantiate(planetprefab,spawnlocation,rotation) as GameObject;
            newplanet.transform.localScale = newplanet.transform.localScale * scale;
            PlanetGenerator.global.planetslist.Add(newplanet);
                Debug.Log ("spawned planet");
            }


            //treespawner(numberoftrees);
            //rockspawner(numberofrocks);

        }
    }

but the while loop doesnt run more than once and the planet always spawns with the first determined location even if the console debugs “false spawn”.

really?

I’ve tweaked the debug statements a little and reduced the random ranges to 10 but I’ve use the above script and it looks to be working fine…

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlanetGenerator : MonoBehaviour
{
    public GameObject startingplanet;
    public static PlanetGenerator global;
    public List<planet> planets = new List<planet>();
    public List<GameObject> planetslist = new List<GameObject>();
    int numberofplanets;
    // Use this for initialization
    void Start()
    {
        PlanetGenerator.global = GetComponent<PlanetGenerator>();
        planetslist.Add(startingplanet);
        numberofplanets = Random.Range(10, 30);
        for (int i = 0; numberofplanets > i; i++)
        {
            Debug.Log("new planet");
            planet newplanet = new planet("forest");
            planets.Add(newplanet);

        }
    }
}
public class planet
{
    public GameObject planetprefab;
    Vector3 spawnlocation;
    //private List<GameObject> planetlist;
    private int scale;

    public planet(string type)
    {
        if (type == "forest")
        {
            //planetlist = PlanetGenerator.global.planetslist; // not used
            planetprefab = Resources.Load("Prefabs/Planet") as GameObject;
            //int numberoftrees = Random.Range(5, 20); // not used yet
            //int numberofrocks = Random.Range(3, 15); // not used yet
            bool spawnedright = false;

            while (spawnedright == false)
            {

                scale = Random.Range(1, 6);
                spawnlocation = new Vector3(Random.Range(-10, 10), Random.Range(-10, 10), 0);
                Debug.Log("generating transform: " + spawnlocation + " Scale: " + scale);
                bool spawning = true;
                for (int a = 0; PlanetGenerator.global.planetslist.Count > a; a++)
                {
                    GameObject theplanet = PlanetGenerator.global.planetslist[a];
                    Vector2 planetlocation;
                    planetlocation.x = theplanet.transform.position.x;
                    planetlocation.y = theplanet.transform.position.y;
                    float theplanetradious = theplanet.transform.GetChild(0).GetComponent<CircleCollider2D>().radius;
                    float secondplanetradious = theplanet.transform.GetChild(0).GetComponent<CircleCollider2D>().radius * scale;
                    if (Vector2.Distance(planetlocation, spawnlocation) < theplanetradious + secondplanetradious)
                    {
                        spawning = false;
                        Debug.Log("false spawn: " + spawnlocation);
                    }
                }
                if (spawning == true)
                {
                    spawnedright = true;
                }
            }

            if (spawnedright == true)
            {
                Quaternion rotation = new Quaternion(0, 0, 0, 0);
                GameObject newplanet = GameObject.Instantiate(planetprefab, spawnlocation, rotation) as GameObject;
                newplanet.transform.localScale = newplanet.transform.localScale * scale;
                PlanetGenerator.global.planetslist.Add(newplanet);
                Debug.Log("spawned planet: " + spawnlocation + " Scale:" + scale);
            }


            //treespawner(numberoftrees);
            //rockspawner(numberofrocks);

        }
    }
}

it seems to work fine in the console but i think there may be problem with the scale because sometimes a big planets orbit collides with a small planets orbit

look to be right there… looks like you need to work out the scale and include it into line 57… got a planet here with scale 4 and the collider radius is still 0.5.

// since planets are uniformly scaled can just grab the x as the scalar scale value
float planetScale = theplanet.transform.localScale.x;
float theplanetradious = theplanet.transform.GetChild(0).GetComponent<CircleCollider2D>().radius * planetScale;
1 Like

Oh ok i solved it. Thank you for all the help :slight_smile: