Search Unity

Variable in other script is null all the time

Discussion in 'Scripting' started by martis941, Oct 21, 2016.

  1. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    What lie does the error say?
     
  3. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    line 198 wizard.IncreaseStats(6, 6, 6); // doesnt get called

    and it should call this method
    Code (CSharp):
    1. public void IncreaseStats(int id,int buff_amount,int buff_duration)
    2.     {
    3.         print("attackdamage" + attackDamage);
    4.         attackDamage+= buff_amount;
    5.         currentPlayerHealth += buff_amount;
    6.         //STAT ID, BUFF amount, BUFF DURATION
    7.         print("attackdamage after increase" + attackDamage);
    8.     }
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    What line does the error tell you the problem is on?
     
  5. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    i just answered , scroll up
     
  6. Endalth

    Endalth

    Joined:
    Oct 13, 2016
    Posts:
    11
    Does your gameobject have a Wizard script attached to it? Line 21( wizard = GetComponent<Wizard>(); ) might be making it null.
     
  7. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    yes , in the inspector i dragged in script which is on the wizard
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What do you mean you dragged in the script? Does your gameobject have both the Wizard script and the Inventory script on it? (And not just dragged into the variable slot, but actually have the script on the object itself)
     
  9. Endalth

    Endalth

    Joined:
    Oct 13, 2016
    Posts:
    11
    By "on the wizard", do mean the little inspector box created for the wizard variable? If that's what you mean that won't work. You need to have the component on the object for GetComponent method to find it.
     
  10. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    i mean that
     

    Attached Files:

  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    That is why it's null. GetComponent looks at the same gameobject as the inventory script is on and says, find me a Wizard script. In this case, you have no wizard script so it sets the value to null. You either need to have the inventory script on an object that has the WIzard script or you need to comment out the line that @Endalth referenced

    EDIT: GetComponent without a target does this. if you had say targetPlayer.GetComponent, that would look at the targetPlayer gameObject to look for the script.
     
  12. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    so i need gameobject reference?
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If you have
    Code (CSharp):
    1. public GameObject playerWizard;
    2. private Wizard wizard;
    3.  
    4. void Start()
    5. {
    6.    wizard = playerWizard.GetComponent<Wizard>();
    7. }
    Then that can work also. That's assuming you drag and drop your gameObject with your Wizard script into the playerWizard slot. (Or find it some way with like GameObject.Find. or something.)
     
  14. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    Thanks a lot ! everything works perfect now