【发布时间】:2017-12-19 16:49:28
【问题描述】:
我只是无法将公共对象添加到我拥有的 dcript 中。 有代码:
public class Player : MonoBehaviour {
public string charName = "";
public int currentLevel = 0;
public int experiense = 0;
public int strength = 1;
public int agility = 1;
public int maxHealth = 30;
public float currentHealth = 30;
public int maxActionPoints = 5;
public int currentLevelPoints = 10;}
还有一个脚本,我想用这个类添加一个公共属性
public class CharManager : MonoBehaviour {
public GameObject currentCharacter;
public GameObject charMenu;
public Player currentPlayerStats;
public void changeCharacter(GameObject character)
{
if (currentCharacter){
saveCharacter ();
}
currentCharacter = character;
loadSavedInfo ();
}
void loadSavedInfo()
{
string playerJson = "";
if (currentCharacter.tag== "Man")
{
if (File.Exists(Application.persistentDataPath +"/Char1.json"))
{
playerJson = File.ReadAllText(Application.persistentDataPath +"/Char1.json");
}
}
else
{
if (File.Exists(Application.persistentDataPath +"/Char2.json"))
{
playerJson = File.ReadAllText(Application.persistentDataPath +"/Char2.json");
}
}
if (playerJson != string.Empty)
{
Player thePlayer = JsonConvert.DeserializeObject<Player>(playerJson);
currentPlayerStats = thePlayer;
}
else
{
currentPlayerStats = gameObject.AddComponent<Player>() as Player;
}
}
这段代码添加了新的播放器组件,并且 currentPlayerStats 有类 CharManager...我做错了什么? 非常感谢任何帮助!
【问题讨论】:
-
从您在此处复制的代码看来,Player 类不应该是 MonoBehaviour..?它只保存统计值,也许试着让它不是 MonoBehaviour ?
-
我确实尝试过这样做。那么我怎么能从另一个脚本中获取这个属性呢? GetComponent 不起作用。