Question Crash

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
When I assign this script to a gameobject Unity crashes. Does anyone know why?

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveGene : MonoBehaviour
{
public int fed = 0;
public int dayTime = 3;
public int FoodCap = 1;
public Rigidbody rb;
public GameObject food;
public GameObject Organism;
public float scope = 1f;
public float speed = 1f;
public bool isHome = false;
public Vector3 Home;
public float hRange = 0.5f;
private float startTime;
public float t;


// Start is called before the first frame update

void Start()
{


startTime = Time.time;
        Home = rb.position;

    }

    private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Consumable"))
{
fed++;
}
    }


    // Update is called once per frame
void Update()
{
while ( Organism != null)
        {

        }

         t = (Time.time - startTime);
if (food == null)
{
           
            float n = (rb.transform.position.z - Home.z) / (rb.transform.position.x - Home.x);
            if (rb.transform.position.x > Home.x && isHome == false) rb.AddForce(-speed * Time.deltaTime, 0, -n * speed * Time.deltaTime);
            else if (rb.transform.position.x < Home.x && isHome == false) rb.AddForce(speed * Time.deltaTime, 0, n * speed * Time.deltaTime);
          


            if ((Mathf.Abs(rb.transform.position.z - Home.z) < hRange && Mathf.Abs(rb.transform.position.x - Home.x) < hRange))
{
rb.velocity = Vector3.zero;
isHome = true;
}




}
if (food != null)
{
while (Mathf.Abs(rb.transform.position.x - food.transform.position.x) > scope && Mathf.Abs(rb.transform.position.z - food.transform.position.z) > scope)
{
int Vel = Random.Range(-100, 100);
int Vel2 = Random.Range(-100, 100);
rb.AddForce(speed*Vel*Time.deltaTime, 0, speed*Vel2*Time.deltaTime);

}

if (Mathf.Abs(rb.transform.position.x - food.transform.position.x) < scope && Mathf.Abs(rb.transform.position.z - food.transform.position.z) < scope)
            {

float k = (rb.transform.position.z - food.transform.position.z) / (rb.transform.position.x - food.transform.position.x);
if (rb.transform.position.x > food.transform.position.x) rb.AddForce(-speed*Time.deltaTime, 0, -k * speed*Time.deltaTime);
                else rb.AddForce(speed*Time.deltaTime, 0, k * speed*Time.deltaTime);

               

            }

}
if (t > dayTime)
{

if (fed > 0 && isHome == true)
{


}
else Destroy(gameObject);
}

}
}
 
Perhaps you could provide a more detailed explanation of what actually happens. I don't use Unity but I may be able to help if I have all the relevant information. I may be able to identify the issue with the code if I have some idea what I'm looking for but there's a fair bit of code there and it is quite poorly formatted, so I'm not prepared to just start trawling through it in the hope that something jumps out.
 
Back
Top Bottom