- Home /
Variable from other class always returns 0
I'm trying to call an int from another class and I've tried everything, from instancing to creating a new instance using the = new.... Considering this is scripting and not straight up class-making i guess it's different because it attaches to an object.
Here's my problem, I have a really nice stat system set up, alright, not really nice, but for just starting out with unity I like it. Either way, I'm going to send you the basics of the code and see if you guys can help
StatHandler.cs
public class StatHandler : MonoBehaviour {
public int stamina, strength, agility, dexterity, wisdom;
public int baseHealth, baseMana;
public int statPoints;
public bool showingStatGUI;
#region start
void Start() {
//TODO Class Based Stats
stamina = 10;
strength = 10;
agility = 10;
dexterity = 10;
wisdom = 10;
baseHealth = stamina * 10;
baseMana = wisdom * 10;
statPoints = 100;
showingStatGUI = false;
}
#endregion
#region getters
public int Stamina {
get { return stamina; }
set { stamina = value; }
}
I tried using a getter this time to return the value and still have no luck, please remember that on the StatHandler itself the variables do have values, which I can edit with the gui I created.
Here's the other part
HealthManager.cs
public int stamina;
public float healthBarLength;
// Use this for initialization
void Start () {
StatHandler sh = new StatHandler();
healthBarLength = Screen.width / 2;
stamina = sh.Stamina;
}
Please note, the above was just an attempt using the setter/getters, it tossed a compiler error without creating a new StatHandler, this is really irritating and my only problem with creating many scripts.
On a side note, after glimpsing at your profile:
$$anonymous$$oderation-wise, please accept answers which helped you and don't be shy to thumb up. It's a karma forum, and part of making this forum successful is using this system.
Also, please sum up conclusions or notes in comments. The answers aren't designed to be used for that.
Cheers! :)
Yes, thanks. I noticed that when using stack-overflow today.
Answer by GuyTidhar · Jan 13, 2013 at 12:04 PM
Since you are inheriting from MonoBehavior, you can not use new,
so "StatHandler sh = new StatHandler();" is not an option.
By inheriting from MonoBehavior, you defined a unity Component for unity.
You have the option of either instantiate your class as part of the prefab object to which it is attached to: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Or add the component to an existing or newly created GameObject: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html
(Newly create a GameObject and add component: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GameObject.html)
You sir, have saved my day. I've only been using Unity for around a week now and never really thought of it this way... I'm going to try my luck doing this and I'll get back to you. Thumbs up for now.
So, would I have to create my entire player GameObject through a script? perhaps I'm not understanding this correctly. Can I create a script that adds components and attatch it to a GameObject I already have created?
I figured it out, although it's still returning a value of 0 for any integer i try to full from StatHandler.
Glad to had helped buddy!
$$anonymous$$ust likely, you would create a prefab - A game object with components already attached and set and any number of root game objects (themselves possibly parents of other game objects) - all of which are saved in the project level and can be referenced to the managing GameObject: http://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html
You can also create a script that adds components and attach it to a game object you already created.
Well, I do have one problem that's really confusing me. Is there a way I could message you? I'm still having trouble retrieving the integer value, it's hitting 0. I wouldn't $$anonymous$$d sending you my scripts if you could point me in the right direction, I don't know what I'm doing wrong, but I'd love to find out. I've been googling for hours.