Home > Software design >  Beginner to c# having issues with CS0131: The left-hand side of an assignment must be a variable, pr
Beginner to c# having issues with CS0131: The left-hand side of an assignment must be a variable, pr

Time:08-06

I'm rather new to coding in C# and am trying to teach myself more. I've been trying to work on a simple rpg with stats from levels in RPG and have gotten stuck with trying to apply the damage to an enemy based on my characters stats.

When I thought I had solved the issue by splitting my stats script for the player into a second one for the enemy units, I unfortunately ran into the issue where the left-hand side of the assignment needing to be a variable property or indexer, and no matter how much I search for solutions I'm stumped. Would anyone be able to take a look at my scripts and point out any glaring errors I've made?

Please and thank you!

   public void TakePhysicalDamage()
    {
    defaultStats.GetPhysicalDamage()-= armor; //This is the offending line
    physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
    health -= (int)Math.Round(physicalDamage);   
    
    if(health <= 0)
        {
        health = 0;
        Die();
    }
}
void Die()
{
    {
        playerLevel.AddExperience(experience_reward);
    }
    Destroy(gameObject);
}

}

Here is the playerstats (defaultstats) script just for reference where I'm trying to get the physical damage from

[SerializeField] float strength = 5f; [SerializeField] float physicalDamage = 5f;

  public float GetPhysicalDamage()
  {
    return physicalDamage  = strength;
  }

Sorry if this seems super basic, but please take a look if you're bored!

CodePudding user response:

You are trying to modify a function:

defaultStats.GetPhysicalDamage()-= armor;

but you can't, because GetPhysicalDamage only returns damage, it isn't setup as a property that would allow you to modify it (and also don't do that!)

public float GetPhysicalDamage()
{
  return physicalDamage  = strength;
}

Instead, it looks like you have a variable physicalDamage that you should be using instead, like:

public void TakePhysicalDamage()
{
    physicalDamage = defaultStats.GetPhysicalDamage() - armor; //This is the offending line
    physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
    health -= (int)Math.Round(physicalDamage);   
    
    if(health <= 0)
    {
        health = 0;
        Die();
    }
}

Actually, on closer review, I think you're probably not doing what you think you're doing. It looks like physicalDamage is supposed to be the base damage you're doing, but when you have a line like the following from GetPhysicalDamage():

return physicalDamage  = strength;

if physicalDamage is 5 and strength is 5, then the first time you call GetPhysicalDamage() you get 10. BUT what you're doing is adding the strength to physical damage and storing that value as the new physical damage with the = operator, such that the next time you call GetPhysicalDamage() the physicalDamage variable is now 10 (from the previous call) and now it returns 15. Then 20, 25, etc.

I think what you want instead is just the sum of physicalDamage and strength, like:

return physicalDamage   strength;

But if this is the case then I think the variable name physicalDamage is misleading. I would personally prefer something like basePhysicalDamage and then you can have a property like:

public int PhysicalDamage => basePhysicalDamage   strength;

I would especially recommend doing this because later in your code, where you're having the issues now, you are modifying the physicalDamage variable on lines like:

physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);

This is also confused because it looks like you're trying to GetPhysicalDamage and modify that with armor, but when you call GetPhysicalDamage and armor you're getting them from the same (local) source, so it would be either the player's physical damage dealt to themselves with the player's armor, or it would be the mob's physical damage dealt to themselves with their armor.

I would pass the damage as an argument so that you can send damage from one thing to another, like:

public void TakePhysicalDamage(int damage)
{
    damage -= armor;
    damage = Mathf.Clamp(damage, 0, int.MaxValue);
    health -= (int)Math.Round(damage);
    if(health <= 0)
    {
        health = 0;
        Die();
    }
}
  • Related