Search Unity

Accesing bools from a script with inactive object

Discussion in '2D' started by Orinuvan, Sep 10, 2019.

  1. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    63
    Hi

    So I have this project called BattleCore, and I made a character Thor that can throw his hammer.
    I made more parts of this character like torso and legs, and I need to acces somme booleans to specify somme things , and I just dont seem to be able to get them, I used somme FindObjectOfType before and it worked not on bools but other variables and now when I try the syntax it doesnt work it pops this error message :

    NullReferenceException : Object reference not set to an instance od the object.

    I need to check this bools on the body script at the moment of instantiation of the Hammer in a void function.

    I need to acces the bools from the script "Mjolnir" to script "Body".


    Any help is deeply apreciated <3.
     
  2. Adien22

    Adien22

    Joined:
    Aug 19, 2019
    Posts:
    44
    I think this should allow you to access your script.
    Code (csharp):
    1.  
    2. // add this at the top with your other variables and bools
    3. GameObject objectWithScript;
    4.  
    5. // add this in the Start function
    6. objectWithScript.GetComponent<Mjolnir>();
    7.  
    8. // add this or whatever reference you need to do want you want
    9. //in the rest of the script
    10. objectWithScript.Mjolnir.whateveryouwant = whateveryouneed;
    11.  

    let me know if this worked or if I'm just way off from what you need lol