Ich kann es einfach nicht schaffen, ein öffentliches Objekt zu dem Dcript hinzuzufügen, das ich habe. Es gibt Code:Fügen Sie ein benutzerdefiniertes Objekt zum Skript hinzu
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;}
gibt es ein weiteres Skript, wo ich eine öffentliche Eigenschaft mit dieser Klasse hinzufügen möchten
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;
}
}
Dieser Code NEW-Player-Komponente hinzufügen und currentPlayerStats haben Klasse Charmanager ... was bin ich mache falsch? Jede Hilfe wird sehr geschätzt!
Aus dem Code, den Sie hier kopiert haben, scheint die Player-Klasse kein MonoBehaviour zu sein ..? Es enthält nur statistische Werte, vielleicht versuchen Sie es nicht zu einem MonoBehaviour zu machen? – johnkork
Ich habe versucht, das zu tun. Wie könnte ich diese Eigenschaft von einem anderen Skript erhalten? GetComponent funktioniert nicht. – Pavel