- Home /
Calling a variable from another class
I am trying to define an integer "maxHealth" in one script and access its value in another. I don't understand what I'm doing wrong. Here are my scripts.
public class AttributeSys1 : MonoBehaviour
{
#region define variables
//sets all stat integers
public int strengthLevel;
public int speedLevel;
public int intelligenceLevel;
public int willpowerLevel;
public int defenseLevel;
public int maxHealthLevel;
public int maxEnergyLevel;
public int strength;
public int speed;
public int intelligence;
public int willpower;
public int defense;
public int maxHealth;
public int maxEnergy;
#endregion
#region Start Function
// Use this for initialization
void Start ()
{
//set default stat values
strengthLevel = 0;
speedLevel = 0;
intelligenceLevel = 0;
willpowerLevel = 0;
defenseLevel = 0;
maxEnergyLevel = 0;
maxHealthLevel = 0;
}
#endregion
#region SetStats
public void SetStats()
{
strength = ((strengthLevel * 5) + 20);
speed = ((speedLevel * 5) + 20);
intelligence = ((intelligenceLevel * 5) + 20);
willpower = ((willpowerLevel * 5) + 20);
defense = ((defenseLevel * 5) + 20);
maxEnergy = ((maxEnergyLevel * 10) + 100);
maxHealth = ((maxHealthLevel * 10) + 100);
}
#endregion
// Update is called once per frame
void Update ()
{
SetStats();
}
using UnityEngine;
using System.Collections;
public class Vitals : MonoBehaviour {
public int curHealth;
public int maxHealth1;
public int curEnergy;
public int maxEnergy1;
// Use this for initialization
void Start ()
{
maxHealth1 = GetComponent<AttributeSys1>().maxHealth;
curHealth = maxHealth1;
maxEnergy1 = GetComponent<AttributeSys1>().maxEnergy;
curEnergy = maxEnergy1;
}
// Update is called once per frame
void Update () {
}
}
Have you tried making your maxHealth "Extern public maxHealth"?
Are these two scripts on the same gameobject? Do you mean to call SetStats() function in AttributeSys1 during every frame of your game?
I just tried making it "external public health" and I'm getting an error. Yes I have them in the same game object, but when I run the game the first script gives me all my expected values and the second script still returns zero for all of those values defined therein.
Answer by agies1 · Jan 11, 2014 at 04:02 PM
There are a few issues with what you are trying to accomplish;
You are setting your stats in the Update of the Attributes, but trying to collect the values of the Attr. in the Start of the Vital. There is no guarantee that the Update of the Attribute will fire before the Start of the Vital, in fact as they are on the same GameObject all Components will have their Starts fired, before an Frame/Update occur.
Start Order of you scripts is not guaranteed unless you specify it, so even if you set the Attributes in the start, Vitals may still be 0 as Vitals Start may fire before the Attribute. See here on how to force an order Script Execution Order.
Possible Solution Before setting your Vitals from the Attribute, call SetStats to ensure the Attribute has been initialized.
//Getting componets can be expensive, lets cache this result
var attribute1 = GetComponent<AttributeSys1>();
//Force the Attributes to be initialized before accessing them
attribute1.SetStats();
//As @frarees said this is a valueType, so we are copying the value, it will NOT
//Stay in sync with the Attribute's class, if you update Attribue you'll need
//to fetch the value again
maxHealth1 = attribute1.maxHealth;
curHealth = maxHealth1;
maxEnergy1 = attribute1.maxEnergy;
curEnergy = maxEnergy1;
I would also reconsider this
void Update ()
{
SetStats();
}
Update fires every frame, and unless you modifying the Attribute levels every frame, this is incredibly expensive.
Answer by frarees · Jan 11, 2014 at 03:22 PM
int
is a value type i.e. the value it holds is copied into another memory position. There are lots of articles on the net talking about the difference between value and reference types in C# and which types are passed by value or by reference. Try this:
AttributeSys1 as1 = GetComponent<AttributeSys1>();
as1.maxHealth = curHealth;