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.

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

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