![](/img/trans.png)
[英]error: cs0131 The left-hand side of an assingnment must be a variable, property or indexer
[英]Beginner to c# having issues with CS0131: The left-hand side of an assignment must be a variable, property or indexer
我對 C# 中的編碼相當陌生,並且正在嘗試自學更多。 我一直在嘗試使用 RPG 中的關卡統計數據來制作一個簡單的 rpg,並且一直在嘗試根據我的角色統計數據對敵人施加傷害。
當我認為我已經通過將我的玩家統計腳本拆分為敵方單位的第二個腳本來解決了這個問題時,不幸的是我遇到了一個問題,即分配的左側需要是變量屬性或索引器,並且無論我如何尋找解決方案,我都會感到困惑。 任何人都可以查看我的腳本並指出我犯的任何明顯錯誤嗎?
謝謝,麻煩您了!
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);
}
}
這是 playerstats (defaultstats) 腳本,僅供參考,我試圖從中獲得物理傷害
[SerializeField] 浮動強度 = 5f; [SerializeField] 浮動物理傷害 = 5f;
public float GetPhysicalDamage()
{
return physicalDamage += strength;
}
對不起,如果這看起來超級基本,但如果你覺得無聊,請看看!
您正在嘗試修改 function:
defaultStats.GetPhysicalDamage()-= armor;
但你不能,因為GetPhysicalDamage
只返回損壞,它沒有設置為允許你修改它的屬性(也不要這樣做!)
public float GetPhysicalDamage()
{
return physicalDamage += strength;
}
相反,您似乎有一個應該使用的變量physicalDamage
,例如:
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();
}
}
實際上,經過仔細審查,我認為您可能沒有做您認為您正在做的事情。 看起來physicalDamage
應該是你正在做的基礎傷害,但是當你從GetPhysicalDamage()
有如下一行時:
return physicalDamage += strength;
如果physicalDamage
為 5 且strength
為 5,那么您第一次調用GetPhysicalDamage()
時會得到 10。但是您正在做的是增加物理傷害的強度並將該值存儲為使用+=
運算符的新物理傷害,這樣下次調用GetPhysicalDamage()
時, physicalDamage
變量現在是 10(來自上一次調用),現在它返回 15。然后是 20、25 等。
我認為您想要的只是物理傷害和力量的總和,例如:
return physicalDamage + strength;
但如果是這種情況,那么我認為變量名physicalDamage
具有誤導性。 我個人更喜歡basePhysicalDamage
類的東西,然后您可以擁有如下屬性:
public int PhysicalDamage => basePhysicalDamage + strength;
我特別推薦這樣做,因為稍后在您的代碼中,您現在遇到問題的地方,您正在修改physicalDamage
變量,例如:
physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
這也令人困惑,因為看起來您正在嘗試GetPhysicalDamage
並使用armor
對其進行修改,但是當您調用GetPhysicalDamage
和armor
時,您會從同一(本地)來源獲取它們,因此要么是玩家造成的物理傷害用玩家的盔甲對自己造成傷害,或者這將是暴徒用他們的盔甲對自己造成的物理傷害。
我會將損害作為參數傳遞,以便您可以將損害從一件事發送到另一件事,例如:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.