我对统一相当陌生,并且正在尝试基于基于地图盒位置的游戏制作游戏。现在我有一个欢迎场景,他们选择了一个名字和一个角色(男性或女性),效果很好,他们选择了地图上的角色显示。
现在,我有三个脚本:GameManager、Player 和 BonusXP。在 GameManager 中,我正在实例化选择的玩家并将它们放置在地图上。
在播放器脚本中,我有某些变量,例如 xp 和 level 以及 AddXP() 之类的方法。
在我附加到随机对象的bonusXP脚本中,当单击该脚本时,需要向玩家添加一定数量的XP。在这个例子中,我使用了 10。现在我让它工作得很好,直到我添加了选择字符功能。在我将玩家拖入 GameManager 序列化字段之前,一切正常。现在那个人不见了。它停止工作。如何将 XP 添加到用户选择的实例化播放器?
The Gamemanager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : Singleton<GameManager> {
public GameObject[] players;
public Transform spawnPoint;
private void Awake() {
int selectedC = PlayerPrefs.GetInt("selectedcharacter");
GameObject prefab = players[selectedC];
GameObject clone = Instantiate(prefab, spawnPoint.position, Quaternion.identity);
clone.AddComponent<Player>(); //attaches player script to the clone
}
// public Player CurrentPlayer {
// get
// {
// if (currentPlayer == null) {
// }
// return currentPlayer;
// }
// }
}
`
BonusXP 脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class XPBonus : MonoBehaviour {
[SerializeField] private int bonus = 10;
private void OnMouseDown() {
GameManager.Instance.CurrentPlayer.AddXp(bonus);
Destroy(gameObject);
}
}
播放器脚本的必要部分:
public class Player : MonoBehaviour {
[SerializeField] private int xp = 0;
[SerializeField] private int requiredXp = 100;
[SerializeField] private int levelBase = 100;
public void AddXp(int xp) {
this.xp += Mathf.Max(0, xp);
}
}