2017-12-18 14 views
0

私は自分が持っている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;} 

iは

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を持って

このクラスでパブリックプロパティを追加する別のスクリプトがあります: コードがあります私は間違っている? ご迷惑をおかけいたします。

+0

ここでコピーしたコードから、PlayerクラスはMonoBehaviour ..であってはならないと思われます。それは単なる統計値を保持しています、おそらくそれをMonoBehaviourではないようにしようとしますか? – johnkork

+0

私はそれをやろうとしました。だから私は別のスクリプトからこのプロパティを取得できますか? GetComponentは機能しません。 – Pavel

答えて

1

gameObject.AddComponent()は、MonoBehaviour派生コンポーネントをゲームオブジェクトに追加します。プレイヤーはあなただけで、そのクラスのオブジェクト

Player currentPlayerStats = new Player(); 
+0

public class Player:** MonoBehaviour **。それはMonoBehaviourを派生しています。 – Pavel

+0

私はそれを見ていないと信じられない、私は三重チェック、私はとても残念です。 しかし、今私は問題が何かを見ることができません。 –

+0

全く問題ありません。私はそれを解決しました。レスポンスありがとう! – Pavel

0

を作成することができますので、私がそれを考え出した、プレイヤーは普通のクラスであるよう

が見える追加することはできませんので、MonoBehaviourから派生していません。 PlayerクラスはMonoBehaviourクラスでない必要があります。

public Player thePlayer; 

ない

public GameObject thePlayer; 

みんなありがとう:私はプレーヤーのクラスにアクセスする必要があるクラスは、パブリックプロパティを持っている必要があります!

関連する問題