Search Unity

Bug .GetComponent is freezing my code!

Discussion in 'Editor & General Support' started by Fogusss, Feb 8, 2023.

  1. Fogusss

    Fogusss

    Joined:
    Apr 18, 2022
    Posts:
    7
    Whenever .GetComponent runs it will freeze and nothing else will work. How can i fix this?

    Code (CSharp):
    1.  
    2. if (CurrentHp <= 0)
    3.         {
    4.             bar.SetActive(false);
    5.             destScript.enabled = false;
    6.             aiController.enabled = false;
    7.             bcollider.enabled = false;
    8.             isDead = true;
    9.             player.GetComponent<PlayerStats>().CurrentXp += xpGiven; //CODE FREEZEZ HERE
    10.             player.GetComponent<PlayerStats>().enemysKilled += 1; //CODE FREEZEZ HERE
    11.             bombRNG = Random.Range(1, bombmax);
    12.             potionRNG = Random.Range(1, potionmax);
    13.             if(potionRNG == 1)
    14.             {
    15.                 potion = (GameObject)Instantiate(potionPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
    16.                 potion.name = "PotionPickUp";
    17.             }
    18.             if(bombRNG == 1)
    19.             {
    20.                 bomb = (GameObject)Instantiate(bombPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
    21.                 bomb.name = "BombPickup";
    22.             }
    23.         }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    As in Unity completely locks up? Or are you getting a null-ref exception?

    If the former, it means you have an infinite loop happening. If the latter, you just need to figure out why it's null, and how to prevent the null errors.

    Side note, this:
    Code (CSharp):
    1. new Vector3(transform.position.x, transform.position.y, transform.position.z)
    Can just be replaced by using
    transform.position
    .
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    I suspect that "freeze" means "throws an exception" here, yes.

    You can do that because Vector3 is a struct, meaning that it's a value type. Value types aren't a reference to a blob of data somewhere in memory that can be shared; they're just...the value!

    So, you won't wind up with two different variables pointing at the "same" Vector3 (even though I don't think that'd matter here).
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Is that a response to me or extra information to OP here? In my case I was just saying the code can be simplified with the same effect.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    Just extra info. In hindsight, it's probably not very useful, oops :p
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    How do you know it's freezing in that spot in particular?
    There's nothing about a GetComponent that will ever freeze your game, but you could be accessing a property that has an infinite loop in the getter or setter, or you could be Instantiating one of those prefabs lower down that has an infinite loop in its Awake, or is Instantiating recursively in its Awake, and so on.

    So again, I'd be interested to know why you think that line in particular is freezing (and how could both lines be freezing, that makes no sense?) and I'd be interested in seeing the code for the PlayerStats script, as well as any scripts on the prefabs you are instantiating.