Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Tomassias · Oct 30, 2015 at 01:33 PM · properties

Changing property value

Hello, I'm working on a small game - it's my first, and I'm having problem with this. I'm trying to use properties in a class as a storage for how many HP etc. a player base has. However when I change the property from one script, it doesn't affect any other the values of other scripts that use the same property.

This is the class with the properties:

 public class BaseStats {
 
     public int baseHealth = 1000;
 
 
     public int BaseHealth
     {
         get
         {
             return baseHealth;
         }
         set
         {
             baseHealth = value;
         }
     }

This is a script on an enemy that shoud decrease the value on collision

 public class OnHit : MonoBehaviour {
  
     BaseStats baseStats = new BaseStats();
 
     void OnCollisionEnter(Collision col)
     {
 
         if (col.gameObject.tag == "Base")
         {
             baseStats.BaseHealth -= 100;
             Destroy(gameObject);
         }
     }
 }


And this is a script on a Text object that should display current HP

 public class ShowHealth : MonoBehaviour {
     
     public BaseStats baseStats = new BaseStats();
     Text txt;
 
     void Start ()
     {
         txt = GetComponent<Text>();
     }
     
     void Update ()
     {
         txt.text =  baseStats.BaseHealth.ToString();
     }
 }

It displays the default value, which is 1000, but it doesn't change after the enemy collides with base. What am I doing wrong?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Oct 30, 2015 at 01:45 PM

They both have a unique instance of BaseStats each.

ShowHealth.baseStats references one instance of BaseStats
OnHit.baseStats references another instance of BaseStats

Both are assigned a new BaseStats.

You want them both to reference the same instance of BaseStats if you want to share the data. Anyway, this has nothing at all to do with properties. Either get the BaseStats from the other script or create a component that both use instead.

Comment
Add comment · Show 4 · 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 Tomassias · Oct 30, 2015 at 01:52 PM 0
Share

Thanks! I thought that it maybe creates separate instances, but I wasn't quite sure. But I'm not really sure how to make them share. $$anonymous$$y other ideas were to make a script that would create an instance of BaseStats and then try to refence that script in other scripts that change or require the values.

avatar image Statement Tomassias · Oct 31, 2015 at 11:21 AM 0
Share

I'm not really sure how to make them share. $$anonymous$$y other ideas were to make a script that would create an instance of BaseStats and then try to refence that script in other scripts that change or require the values.

1: You'd have to decide who "owns" the BaseStats. It doesn't sound sensible that either of them own it.

2: That's how you share the reference. Or make BaseStats that script.

Either get the BaseStats from the other script or create a component that both use ins$$anonymous$$d.

That's what I suggested. Scripts are components. Perhaps I should have said script :) Create a script that both ShowHealth and OnHit accesses. Consider promoting BaseStats to that actual script.

 public class BaseStats : $$anonymous$$onoBehaviour

Then slap it on the/any game object.

The other scripts declare a public variable like:

  public BaseStats baseStats;

And you can set it via the inspector, or automatically through GetComponent etc.

avatar image Tomassias Statement · Oct 31, 2015 at 12:15 PM 0
Share

Yeah, I dumped that script and made a new one that I put on an empty object, and acces that ins$$anonymous$$d.

 public class ValuesGetSet : $$anonymous$$onoBehaviour {
 
     public int HP,money;
 
     void Start ()
     {
         HP = 1000;
         money = 500;
     }
     
     public int GetHP ()
     {
         return HP;
     }
 
     public void SetHP(int newHP)
     {
         HP = newHP;
     }
 
     public int Get$$anonymous$$oney()
     {
         return money;
     }
 
     public void Set$$anonymous$$oney(int new$$anonymous$$oney)
     {
         money = new$$anonymous$$oney;
     }
 }

Thanks for your help! :)

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

33 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 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

Can I use MaterialPropertyBlock to set properties per material instead of per renderer? 0 Answers

Change the color of a variable field if it is empty 0 Answers

How to make dynamic list custom inspector 0 Answers

Class Inheritance Issue. 0 Answers

Accessing Invalid Property 0 Answers


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