2017-12-18 19 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;} 

내가

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가 아니어야합니다. ..? stat 값만 포함하고 MonoBehaviour가 아닌가? – johnkork

+0

나는 그것을하려고 노력했다. 그래서 어떻게 다른 스크립트에서이 속성을 얻을 수 있습니까? GetComponent가 작동하지 않습니다. – Pavel

답변

1

gameObject.AddComponent()는 MonoBehaviour 파생 된 구성 요소를 게임 객체에 추가합니다. 플레이어는 플레이어 그냥 일반 클래스처럼 그냥 내가 그것을 알아 냈 클래스

Player currentPlayerStats = new Player(); 
+0

공용 클래스 플레이어 : ** MonoBehaviour **. 따라서 MonoBehaviour를 유도합니다. – Pavel

+0

나는 내가 그것을 보지 못했다고 생각할 수 없다. 나는 트리플 체크를했다. 너무 미안하다. 하지만 지금은 문제가 무엇인지 알 수 없습니다. –

+0

전혀 문제가 없습니다. 나는 그것을 해결했다. 응답 해 주셔서 감사합니다! – Pavel

0

의 객체를 생성 할 수 있도록

이 보이는 추가 할 수 없습니다, 따라서 MonoBehaviour에서 파생되지 않습니다. 플레이어 클래스는 MonoBehaviour 클래스가 아니어야합니다.

public Player thePlayer; 

하지

public GameObject thePlayer; 

모두에게 감사 : 내가 플레이어 클래스에 액세스 할 수있는 클래스는 공용 속성을 가져야한다!