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 gagekilmer · Jan 10, 2014 at 11:52 PM · errorvariablesintegerhealth

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 () {
     
     }
 }
 
Comment
Add comment · Show 3
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 KuPAfoo · Jan 11, 2014 at 12:11 AM 0
Share

Have you tried making your maxHealth "Extern public maxHealth"?

avatar image getyour411 · Jan 11, 2014 at 12:14 AM 0
Share

Are these two scripts on the same gameobject? Do you mean to call SetStats() function in AttributeSys1 during every frame of your game?

avatar image gagekilmer · Jan 11, 2014 at 02:58 PM 0
Share

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.

2 Replies

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

Answer by agies1 · Jan 11, 2014 at 04:02 PM

There are a few issues with what you are trying to accomplish;

  1. 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.

  2. 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.

Comment
Add comment · 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
0

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;
Comment
Add comment · 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

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

21 People are following this question.

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

Related Questions

Unable to call variable from transform 2 Answers

Wrong with script 1 Answer

Health not counting down? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

keep getting this error "Unexpected token: 10." any ideas whats wrong? 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