Search Unity

Trying to access variable in another script.

Discussion in 'Scripting' started by Ghujelk, Jun 24, 2016.

  1. Ghujelk

    Ghujelk

    Joined:
    May 21, 2015
    Posts:
    15
    I have two scripts:
    1st script is named "charTemplate"
    2nd script is named "playerManager"

    My first script is attached to an Empty game object in the scene named CharacterManager, and looks like this;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class characterTemplate : MonoBehaviour {
    4.     public static float health = 50f;
    5. }
    My second script is attached to a cube in the scene named PlayerCube.
    I am trying to figure out how to get the value of 'health' from the first script in order to do something like

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class playerManager : MonoBehaviour {
    4.     public GameObject characterManager;
    5.    
    6.     public float playerHealth = 0;
    7.     public float healthAdjForRace = 5f;
    8.    
    9.     void Start () {
    10.     playerHealth = GetComponent<characterTemplate>().health + healthAdjForRace;
    11.     }
    12. }
    I would then drag the CharacterManager empty onto the Character Manager field in the inspector for the PlayerCube.

    The error I'm getting is "Static member 'charcterTemplate.health' cannot be accessed with an instance reference, qualify it with a type name instead"

    This is where I am stuck, when it is asking for a type name, is it looking for the type of value it is, like float? If so, how or where do I let the script know that? If not, what am I missing here?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The static keyword in public static float health = 50f; means that only one of that health variable exist in your entire game. There will never be more than a single health value. This means you do not need to get an object to then reference that objects member variable.
    Code (csharp):
    1. void Start () {
    2.   playerHealth = characterTemplate.health + healthAdjForRace;
    3. }
    If you plan on having multiple characterTemplates, each with their own health value and not a single shared value, then remove the static keywork in your declaration.
    Code (csharp):
    1.  
    2. public class characterTemplate : MonoBehaviour {
    3.     public float health = 50f; // Removed static
    4. }
     
  3. Ghujelk

    Ghujelk

    Joined:
    May 21, 2015
    Posts:
    15
    Thank you for the response, I have been able to access and print out the value of variables in the characterTemplate script via Debug.log commands in the playerManager script while keeping the variables static, hurrah!

    Just so I better understand, because I declared a static variable, any other script(s) that had or changed a health variable would all be referring to that single variable regardless of script.

    And if i were to just declare it just as a public float health, then I would need to use gameObject.getComponent<characterTemplate>().health to specify where to look for the health variable within the script attached to the object I assign in the inspector.