Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LPGaming · Jan 13, 2013 at 11:51 AM · errorreferencinghealth

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GuyTidhar · Jan 13, 2013 at 12:11 PM 0
Share

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! :)

avatar image LPGaming · Jan 13, 2013 at 01:31 PM 0
Share

Yes, thanks. I noticed that when using stack-overflow today.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LPGaming · Jan 13, 2013 at 01:31 PM 0
Share

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.

avatar image LPGaming · Jan 13, 2013 at 01:41 PM 0
Share

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?

avatar image LPGaming · Jan 13, 2013 at 01:57 PM 0
Share

I figured it out, although it's still returning a value of 0 for any integer i try to full from StatHandler.

avatar image GuyTidhar · Jan 13, 2013 at 01:58 PM 1
Share

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.

avatar image LPGaming · Jan 13, 2013 at 02:04 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

why does the script disables itself when referencing something in Awake? 1 Answer

how to call a method from another script using trigger? 1 Answer

Calling a variable from another class 2 Answers

Need help calling a variable from another C# script 1 Answer

Sorry Me again :P Error CS0131 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges