Home > Enterprise >  UNITY - Crashes whenever I try to get a variable from another script
UNITY - Crashes whenever I try to get a variable from another script

Time:07-15

I have this simulation where the "Hunter" scans the area with the scannereyes object inside the hunter for an object called "Apple" and whenever it sees the apple its supposed to hunt after it. This is the same with other hunters from the same prefab as there are more hunters on the same platform.

The scanner eyes is supposed to send the data of the apple's position so the hunter can run for it. Ive tried putting the found apple object into static but that would resolve into every hunter hunting for the same apple so I tried instead doing this method:

Target = GetComponent<ScannerEyes>().foundobject.transform;

And instead of giving the Hunter the data it needed the whole engine just crashes whenever it tries to compile the C sharp code.

Yes everything is well updated and so is my computer. I have a strong enough computer to compile.

//In the Hunter CS file
 
void Update()
    {
        if (reproducerate < 1)
        {
            if (GameObject.FindGameObjectsWithTag("Food") != null ) //All apple objects have the tag "Food"
            {
             
                Target = GetComponent<ScannerEyes>().foundobject.transform;
                directiontolookat = (Target.position - transform.position).normalized;
             
 
                if ((transform.position - Target.position).magnitude > Distans)
                {
                    transform.Translate(directiontolookat * Time.deltaTime * Speed);
                }
            }
        }
    }

public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Food")
        {
            reproducerate  = 1;
            Destroy(collision.gameObject);
            Debug.Log(reproducerate);
        }
    }
//In the ScannerEyes CS file:
 
 
    public bool found = false;
    public GameObject foundobject;
 
    void Update()
    {
//This is only for the scanning sphere to grow overtime cuz thats how scanners work
        if (currentsize != maxsize && !found) {
            transform.localScale = new Vector3(currentsize, currentsize, currentsize);
            currentsize  = scanningspeed;
        }
 
        if (found)
        {
            transform.localScale = new Vector3(0f, 0f, 0f);
        }
    }
 
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Food")
        {
            Debug.Log("WE FOUND IT");
            foundobject = other.gameObject;
            Debug.Log("found at"   foundobject.transform.position);
            found = true;
        }
    }
//In the Food behaviour CS file (This is just to make it spawn in the world between -4, 4)
 
    private GameObject foodobject;
    public GameObject Foodprefab;
    private float Xspawncoords;
    private float Zspawncoords;
   
    void Start()
    {
        Debug.Log("Food is working");
        for (int i = 0; i < 2; i  ){
            Xspawncoords = Random.Range(-4, 4);
            Zspawncoords = Random.Range(-4, 4);
            Debug.Log(Xspawncoords   " and "   Zspawncoords);
            foodobject = Instantiate(Foodprefab,
                new Vector3(Xspawncoords, 1.4f, Zspawncoords),
                Quaternion.identity);
            foodobject.name = "AppleObject";
        }
    }
 

CodePudding user response:

I don't understand if the engine crashes when you start the game or when you just save the scripts.

As far as I can see the Hunter checks that there is at least one GameObject with the "food" tag on it, but you are doing this in the Update() method. This is very bad for performance since you are calling it every frame, and it's pretty expensive as it's constantly checking for every object in the scene.

You can try removing that call and move it in the Start() method, that would call the method only once!

  • Related