Search Unity

Crashes whenever I try to get a variable from another script

Discussion in 'Scripting' started by leyondakenswe, Jul 1, 2022.

  1. leyondakenswe

    leyondakenswe

    Joined:
    Jul 1, 2022
    Posts:
    2
    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 give the placement of the apple so it can hunt 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, obviously.

    Code (CSharp):
    1. //In the Hunter CS file
    2.  
    3. void Update()
    4.     {
    5.         if (reproducerate < 1)
    6.         {
    7.             if (GameObject.FindGameObjectsWithTag("Food") != null ) //All apple objects have the tag "Food"
    8.             {
    9.              
    10.                 Target = GetComponent<ScannerEyes>().foundobject.transform;
    11.                 directiontolookat = (Target.position - transform.position).normalized;
    12.              
    13.  
    14.                 if ((transform.position - Target.position).magnitude > Distans)
    15.                 {
    16.                     transform.Translate(directiontolookat * Time.deltaTime * Speed);
    17.                 }
    18.             }
    19.         }
    20.     }
    21.  
    22. //In the ScannerEyes CS file:
    23.  
    24.  
    25.     public bool found = false;
    26.     public GameObject foundobject;
    27.  
    28.     void Update()
    29.     {
    30. //This is only for the scanning sphere to grow overtime cuz thats how scanners work
    31.         if (currentsize != maxsize && !found) {
    32.             transform.localScale = new Vector3(currentsize, currentsize, currentsize);
    33.             currentsize += scanningspeed;
    34.         }
    35.  
    36.         if (found)
    37.         {
    38.             transform.localScale = new Vector3(0f, 0f, 0f);
    39.         }
    40.     }
    41.  
    42.     private void OnTriggerEnter(Collider other)
    43.     {
    44.         if (other.gameObject.tag == "Food")
    45.         {
    46.             Debug.Log("WE FOUND IT");
    47.             foundobject = other.gameObject;
    48.             Debug.Log("found at" + foundobject.transform.position);
    49.             found = true;
    50.         }
    51.     }
    52.  
    53.  
     
  2. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    333
    I can see a reason for exceptions, but not for a crash, at least not in the shared code. I'm supposing that by "when I try to compile" you are saying when you press play. Normally this kind of crash happens when we forgot to close some loop, can we see more code? Maybe looking for loops or something similar
     
  3. leyondakenswe

    leyondakenswe

    Joined:
    Jul 1, 2022
    Posts:
    2
    Code (CSharp):
    1. //In the hunterspawn behaviour CS file
    2. public GameObject Hunterprefab;
    3.     private GameObject hunter;
    4.     Vector3 Hunter_vel;
    5.  
    6.     private float Xspawncoords;
    7.     private float Zspawncoords;
    8.     int side;
    9.     void Start()
    10.     {
    11.         Debug.Log("Hunterspawn is working");
    12.         for (int i = 0; i < 5; i++)
    13.         {
    14.             side = Random.Range(1,3);
    15.             if (side == 1)
    16.             {
    17.                 Xspawncoords = -18;
    18.                 Zspawncoords = Random.Range(-18, 18);
    19.             }
    20.             else if(side == 2){
    21.                 Xspawncoords = 18;
    22.                 Zspawncoords = Random.Range(-18, 18);
    23.             }
    24.             hunter = Instantiate(Hunterprefab,
    25.                 new Vector3(Xspawncoords, 25f, Zspawncoords),
    26.                 Quaternion.identity);
    27.             hunter.name = "Hunter";
    28.            
    29.             Hunter_vel = hunter.GetComponent<Transform>().position;
    30.          
    31.             Debug.Log("Hunter created at: " + Hunter_vel);
    32.         }
    33.  
    34.     }
    35.  
    36. //In the Food behaviour CS file
    37.  
    38.     private GameObject foodobject;
    39.     public GameObject Foodprefab;
    40.     private float Xspawncoords;
    41.     private float Zspawncoords;
    42.    
    43.     void Start()
    44.     {
    45.         Debug.Log("Food is working");
    46.         for (int i = 0; i < 2; i++){
    47.             Xspawncoords = Random.Range(-4, 4);
    48.             Zspawncoords = Random.Range(-4, 4);
    49.             Debug.Log(Xspawncoords + " and " + Zspawncoords);
    50.             foodobject = Instantiate(Foodprefab,
    51.                 new Vector3(Xspawncoords, 1.4f, Zspawncoords),
    52.                 Quaternion.identity);
    53.             foodobject.name = "AppleObject";
    54.         }
    55.     }
    56.  
    57. //In the hunter CS file for collision check we also have:
    58.  
    59.     public void OnCollisionEnter(Collision collision)
    60.     {
    61.         if (collision.gameObject.tag == "Food")
    62.         {
    63.             reproducerate += 1;
    64.             Destroy(collision.gameObject);
    65.             Debug.Log(reproducerate);
    66.         }
    67.     }
    68.  
    69. /*This is all the code that exists in my project rn*/
    70.