错误 set 访问器无法访问 - 将用户的分数作为货币扣除



我有一个分数管理器,可以跟踪你的分数。有了这些要点,我希望用户能够为他的角色购买升级。

但是每次我尝试访问用户的当前分数时,我都会收到相同的错误消息。

如果您查看下面的升级菜单文档,您会发现我正在尝试执行以下操作"//ScoreManager.Score -= upgradeCost;//之所以存在,是因为当我激活它时,我收到错误消息:

属性或索引器"ScoreManager.Score"不能在此上下文中使用,因为 set 访问器不可访问。

得分管理器


using UnityEngine;
using System;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance { get; private set; }
public static int Score { get; private set; }
public int HighScore { get; private set; }
public bool HasNewHighScore { get; private set; }
public static event Action<int> ScoreUpdated = delegate {};
public static event Action<int> HighscoreUpdated = delegate {};
private const string HIGHSCORE = "HIGHSCORE";
// key name to store high score in PlayerPrefs
void Awake()
{
if (Instance)
{
DestroyImmediate(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
void Start()
{
Reset();
}
public void Reset()
{
// Initialize score
Score = 0;
// Initialize highscore
HighScore = PlayerPrefs.GetInt(HIGHSCORE, 0);
HasNewHighScore = false;
}
public void AddScore(int amount)
{
Score += amount;
// Fire event
ScoreUpdated(Score);
if (Score > HighScore)
{
UpdateHighScore(Score);
HasNewHighScore = true;
}
else
{
HasNewHighScore = false;
}
}
public void UpdateHighScore(int newHighScore)
{
// Update highscore if player has made a new one
if (newHighScore > HighScore)
{
HighScore = newHighScore;
PlayerPrefs.SetInt(HIGHSCORE, HighScore);
HighscoreUpdated(HighScore);
}
}
}

升级菜单


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UpgradeMenu : MonoBehaviour
{
[SerializeField]
private Text accuracyText;
[SerializeField]
private Text speedText;
[SerializeField]
private Text damageText;
[SerializeField]
private Weapon weapon;
[SerializeField]
public Projectile projectile;
[SerializeField]
private Player player;
[SerializeField]
private int upgradeCost = 50;
void start ()
{
}
void OnEnable()
{
UpdateValues();
}
void UpdateValues ()
{
}
public void UpgradeArmor ()
{
Health.maxHealth += 2;
//  ScoreManager.Score -= upgradeCost;
UpdateValues();
}
public void UpgradeSouls ()
{
EnemySlime.ScoreOnDeath += 1;
EnemySkeleton.ScoreOnDeath += 1;
//  ScoreManager.Score -= upgradeCost;
UpdateValues();
}
public void UpgradeDamage ()
{
Projectile.DamageOnHit += 1;
//  ScoreManager.Score -= upgradeCost;
UpdateValues();
}
}

如果类的 set 访问器设置为私有,则无法从类外部为分数分配值。 将其设置为public或在ScoreManager类上创建一个方法来执行推导。

最新更新