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 AliceWolf · Mar 30, 2019 at 07:37 PM · settersgetters

Getter working, Setter not

I'm creating a couple of scripts for an assessment and utilising encaptualisation, I'm running into an issue where code from my class script isn't being called when my tester script runs. I'm not sure what the missing link is that I need to get it to output the info being declared in my class script.

class script

public class OrcInfantry { //Player Name public string playerName = "Orc Infantry";

 //Player Health
 private int _playerHealth = 100;

 //PlayerDamage
 private int _playerDamage = 30;

 //returns player health when called
 public int PlayerHealth
 {
     get
     {
         return _playerHealth;
     }
 }
 
 
 //player Damage Modifier
 // on crit player damage is multiplied by 2
 private bool criticalHit = true;
 public int PlayerTakeDmg
 {
     get
     {
         return _playerDamage;
     }

     set
     {
         if(criticalHit == true)
         {
            _playerHealth = (_playerHealth - value * 2);
         }
     }
 }
 

}

tester script

public class OrcTester : MonoBehaviour { private OrcInfantry _orcInfantry;

 // Use this for initialization
 void Start ()
 {
     //just to show the script is running
     Debug.Log("Zug Zug.");

     //"New" Is creating a new object from a class
     _orcInfantry = new OrcInfantry();

     //Asks for the player's name
     Debug.Log("Who are you? ");
     Debug.Log("I am " + _orcInfantry.playerName);

     //shows the player's health
     Debug.Log("Show current health: " + _orcInfantry.PlayerHealth);

     //shows the unmodified damage value
     Debug.Log("Show current damage: " + _orcInfantry.PlayerTakeDmg);

     //shows the modified health value
     Debug.Log("Player take damage...");
     Debug.Log("Show new health: " +_orcInfantry);


 }

 // Update is called once per frame
 void Update () {
     
 }

}

all this needs to do is output some values to the console. For the last part what I think I should be seeing is the health being set to 40 but all it is doing is showing me the health as 100 again.

note: I don't need to drastically rewrite this script, it's for an assignment so the way it's formatted is basically how it needs to be submitted. I just need some help in figuring out why this part of the script isn't getting called and if I'm just not using the right commands to call it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 30, 2019 at 07:56 PM

Sorry but i don't quite get the exact issue. You never actually invoke the setter of your property. Also what you've done here is the prime example of abusing properties. It looks like you don't really understand the point of properties. You abuse the setter of "PlayerTakeDmg" like an ApplyDamage method. The getter of that property is completely unrelated to what your setter does. Your setter actually modifies a completely unrelated field.


Anyways, to actually invoke the setter of a property you have to actually assign / set a value to that property. Something like

 _orcInfantry.PlayerTakeDmg = 20;


This will invoke the setter with the value being 20. Since this is an assignment you really should rethink about your usage of properties. Any serious instructor wouldn't be pleased to see such an abuse of properties ^^.

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 AliceWolf · Mar 30, 2019 at 09:54 PM

@Bunny83 I am still learning so yeah you’re probably right and this is probably an abuse of properties. I came here for help and advice not to be berated about how this code has been written. You should be mindful of how you word responses as you’ve come across quite rude. Everyone is at different levels of learning. Not everyone will have the same understanding as you and it’s not very encouraging if you’re attaching someone’s methods when all they’ve asked for is help and a little clarity in something that is by no means easy.

It was modeled after an in class exercises created and written by my lecturer who has years of experience programming for games. If it works and isn’t throwing errors what’s the issue? Thank you for your suggestion, i’ll Give that a try and see if it produces a result close to what I’m trying to achieve.

As for the getter/setter in my code. How it was explained to me is that the getter will “get” a value. In my case it’s getting 30 from private int _playerDamage = 30; It then can pass that to a setter through the use of Value. What I’m trying to do is take that number to apply a damage debuff to the player where it takes 60 damage not 30. So what I believe should be happening is _playerHealth should be subtracted by 60 giving me an output of 40 to the console. I don’t believe there’s any issue with how this code is written and rather with how the OrcTester script is written. And if I am missing something there to actually use that part of the code.

And not that it matters but my lecturer has looked over this code and said himself it is fine and that I’m on the right track for this assignment. With this obvious issue aside. However he can’t just give me the answer to fixing it for obvious reasons.

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

168 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Getting a variable works in a Start function, but not in another one. 1 Answer

Why are getters getting called more than they should be called 0 Answers

Weird setter functionality? 1 Answer

Dictionary set value with dynamic keys 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