Unity breaks when execute this code

I’m developing a project at the university where study, I’m having a problem running my code.

Basically, I’m doing a gravity simulator, where each star is affected by the movement of each other. I made a code where it runs, however, when I run the project the unity breaks down, and I have to close the file editor.

Here the code, and sorry for my bad English.

using UnityEngine;
using System.Collections;

public class movimento : MonoBehaviour
{
    //variaveis para encontrar a instancia das demais estrelas
    private GameObject outraEstrela;
    private int contprox = 0;
   
    //variaveis auxiliares para realização de calculos
    private float deltaX, deltaY, distancia, aceleracaoX, aceleracaoY;

    //propriedades da estrela
    private float velocidadeY, velocidadeX, massa, raio;

    // Use this for initialization
    void Start()
    {
        velocidadeY = 0;
        velocidadeX = 0;
    }
   
    // Update is called once per frame
    void Update () {
        outraEstrela = GameObject.Find("estrela"+contprox);
        while(outraEstrela!=null){
            if(this.GetInstanceID() == outraEstrela.GetInstanceID()){
                contprox++;
            }else {
                contprox++;
                deltaX = outraEstrela.transform.position.x - this.transform.position.x;
                deltaY = outraEstrela.transform.position.y - this.transform.position.y;
                distancia = Mathf.Sqrt(Mathf.Pow(deltaX, 2f)+Mathf.Pow(deltaY, 2f));
                aceleracaoX=(deltaX)/Mathf.Pow(distancia, 3f);
                aceleracaoY=(deltaY)/Mathf.Pow(distancia, 3f);
            }
        }
        velocidadeX += aceleracaoX*Time.deltaTime;
        velocidadeY += aceleracaoY*Time.deltaTime;
        this.transform.Translate(new Vector3(velocidadeX, velocidadeY, 0)*Time.deltaTime,Space.Self);
        contprox=0;
    }

}

Unity crashes because it hangs inside your while() indefinitely.

Try:

void Update () {
        outraEstrela = GameObject.Find("estrela"+contprox);
        while(outraEstrela!=null){
            if(this.GetInstanceID() == outraEstrela.GetInstanceID()){
                contprox++;
            }else {
                contprox++;
                deltaX = outraEstrela.transform.position.x - this.transform.position.x;
                deltaY = outraEstrela.transform.position.y - this.transform.position.y;
                distancia = Mathf.Sqrt(Mathf.Pow(deltaX, 2f)+Mathf.Pow(deltaY, 2f));
                aceleracaoX=(deltaX)/Mathf.Pow(distancia, 3f);
                aceleracaoY=(deltaY)/Mathf.Pow(distancia, 3f);
            }

            outraEstrela = GameObject.Find("estrela"+contprox);  // <- added line here
        }
        velocidadeX += aceleracaoX*Time.deltaTime;
        velocidadeY += aceleracaoY*Time.deltaTime;
        this.transform.Translate(new Vector3(velocidadeX, velocidadeY, 0)*Time.deltaTime,Space.Self);
        contprox=0;
    }

Basically you searched for outraEstrela gameObject only once, hence it constantly looped because you didn’t search for the next one.

EDIT: Also I would recommend you to use an array to store all your gameObjects, either GameObject[ ] if fixed size or List if dynamic.

That way you can easily iterate through all your objects, much faster, more readable and less error prone in case they have different names. Not to mention avoiding indefinite loops >_>

void Update ()
{
        GameObject[] list = ...   // <-- provide a list of all relevant gameObjects
        for (int i = 0; i < list.Length; i++)
        {
             GameObject outraEstrela = list[i];
             if ( this.GetInstanceID() != outraEstrela.GetInstanceID() )  // i'm sure a (gameObject != outraEstrela) would work but ok
             {
                deltaX = outraEstrela.transform.position.x - this.transform.position.x;
                deltaY = outraEstrela.transform.position.y - this.transform.position.y;
                distancia = Mathf.Sqrt(Mathf.Pow(deltaX, 2f)+Mathf.Pow(deltaY, 2f));
                aceleracaoX=(deltaX)/Mathf.Pow(distancia, 3f);
                aceleracaoY=(deltaY)/Mathf.Pow(distancia, 3f);
             }
        }      
     
        velocidadeX += aceleracaoX*Time.deltaTime;
        velocidadeY += aceleracaoY*Time.deltaTime;
        this.transform.Translate(new Vector3(velocidadeX, velocidadeY, 0)*Time.deltaTime,Space.Self);
}