Unity 中的硬币系统
Coin System in Unity
我在主菜单中制作了一个硬币系统(在Unity C#中),所以我可以为他们购买物品。我想在游戏场景中捡起硬币并添加到这个分数中,我在主菜单和游戏场景中也能看到。
在游戏管理器中:
public Text coinText;
玩家控制器脚本:
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManager>();
当玩家拿起硬币时:
`
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
MainGameManager.Instance.currency += 1;
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
}
在层次结构中我制作了一个 UI -> Text 并放入了 GameManager。
我在游戏场景中看到的是:货币:但是我没有看到我有多少没有加到货币中。
在 PlayerController 脚本中,您在尝试设置文本后定义 gameManagerScript。
您的 UI 可能尺寸不合适(不适合您的所有文字)
如果 none 是答案,请尝试在控制台中使用 print(string)
或 Debug.log(string)
进行调试,而不是 ui
您可以使用静态字段。
在您的 GameManager 中:
public static int coins;
在播放器中:
GameManager.coins += val
您还可以在 MainMenu 脚本中使用 GameManager.coins 访问该值。
Note:If 您正在尝试使此数据在不同的运行中保持不变,您可以使用 PlayerPrefs class:
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
我在主菜单中制作了一个硬币系统(在Unity C#中),所以我可以为他们购买物品。我想在游戏场景中捡起硬币并添加到这个分数中,我在主菜单和游戏场景中也能看到。
在游戏管理器中:
public Text coinText;
玩家控制器脚本:
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManager>();
当玩家拿起硬币时: `
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
MainGameManager.Instance.currency += 1;
gameManagerScript.coinText.text = "Currency : " + MainGameManager.Instance.currency.ToString();
}
在层次结构中我制作了一个 UI -> Text 并放入了 GameManager。
我在游戏场景中看到的是:货币:但是我没有看到我有多少没有加到货币中。
在 PlayerController 脚本中,您在尝试设置文本后定义 gameManagerScript。
您的 UI 可能尺寸不合适(不适合您的所有文字)
如果 none 是答案,请尝试在控制台中使用 print(string)
或 Debug.log(string)
进行调试,而不是 ui
您可以使用静态字段。 在您的 GameManager 中:
public static int coins;
在播放器中:
GameManager.coins += val
您还可以在 MainMenu 脚本中使用 GameManager.coins 访问该值。 Note:If 您正在尝试使此数据在不同的运行中保持不变,您可以使用 PlayerPrefs class: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html