2016-11-20 7 views
-1

그래서는 저장 및로드 스크립트를 만드는거야으로, 여기 콘텐츠의 것 개체의 인스턴스를 참조하도록 알려줍니다 :없이유니티, 비주얼 스튜디오는 나에게 말한다하지

using UnityEngine; 
using System.Collections; 

public class SaveLoad : MonoBehaviour { 

public Player player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); 

public void Save() { 

    PlayerPrefs.SetFloat ("X", transform.position.x); 
    PlayerPrefs.SetFloat ("Y", transform.position.y); 
    PlayerPrefs.SetFloat ("Z", transform.position.z); 

    PlayerPrefs.SetFloat("Health", Player.Health.CurrentVal); 
    PlayerPrefs.SetFloat ("Exp", Player.Exp.CurrentVal); 
    PlayerPrefs.SetFloat("Oxygen", Player.Oxygen.CurrentVal); 
    PlayerPrefs.SetFloat("PlayerLevel", Player.PlayerLevel.CurrentVal); 

    Debug.Log ("Saved"); 


} 

public void Load() { 
    float x = PlayerPrefs.GetFloat ("X"); 
    float y = PlayerPrefs.GetFloat ("Y"); 
    float z = PlayerPrefs.GetFloat ("Z"); 

    Player.Health.CurrentVal = PlayerPrefs.GetFloat("Health"); 
    Player.Exp.CurrentVal = PlayerPrefs.GetFloat("Exp"); 
    Player.Oxygen.CurrentVal = PlayerPrefs.GetFloat("Oxygen"); 
    Player.PlayerLevel.CurrentVal = PlayerPrefs.GetFloat("PlayerLevel"); 

    transform.position = new Vector3 (x, y, z); 

    Debug.Log ("Loaded"); 
} 
} 

첫 번째 버전은 " Public Player player "라인은 NullReferenceException을 반환하고 Unity에서 객체의 인스턴스를 통해 참조한다고 말했습니다. 이제 ("Health", Player.Health.CurrentVal) 대신 "Player"에 자본이없는 "Health", player.Health.CurrentVal)을 사용하려고 시도했을 때 내가 살았던 인스턴스를 참조합니다. 같은 스크립트), Visual은 "Player.Health는 객체의 인스턴스로 액세스 할 수 없으며 대신 유형 이름으로 한정합니다"라고 알려줍니다. 보시다시피, 저는 여기 루프에 좀 붙어 있습니다. 나는 플레이어를 인스턴스로 만들려고 노력하는 것을 망 쳤니? 플레이어가 게임 개체의 이름과 스크립트의 두 가지 원인이기도합니다. 플레이어 스크립트 : 여기

[SerializeField] public Stat health; 
public static Stat Health { get; set; } 
[SerializeField] private Stat exp; 
public static Stat Exp { get; set; } 
[SerializeField] private Stat oxygen; 
public static Stat Oxygen { get; set; } 
[SerializeField] private Stat playerLevel; 
public static Stat PlayerLevel { get; set; } 
[SerializeField] private Text playerLevelText; 

그리고는 합계에게 사용자 정의 변수 클래스입니다 :

[SerializeField] private BarPrototype bar; 
[SerializeField] private float maxVal; 
[SerializeField] private float currentVal; 

public float CurrentVal 
{ 
    get 
    { 
     return currentVal; 
    } 
    set 
    { 
     this.currentVal = Mathf.Clamp (value, 0, MaxVal); 
     if(bar != null) 
      bar.Value = this.currentVal; 
    } 
} 

SerializeField이 관리자에서 해당 값에 액세스 단지 사용된다.

public Player player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); 

그들은 시작, 깨어 또는 OnEnable 방법으로 초기화 할 필요가

+0

'Player' 클래스에서 관련 부분을 게시 할 수 있습니까? –

+0

물론, 첫 번째 게시물 @ botond.botos – Nech

답변

2

문제는이 같은 공용 멤버를 초기화 할 수 있습니다. 아래로 변경하면 제대로 작동합니다.

public Player player; 
void Start(){ 
    player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); 
} 

또 다른 이유는 공개로 설정해야하는 이유입니다. Inspector에 할당 된 경우 Start() 후에 할당 된 값이 무시됩니다. 나는 당신이 그것을 내부로 만들 것을 제안한다. 또는 안전 :

public Player player; 
void Start(){ 
    if(player == null) 
     player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); 
} 
+0

"이제는 ("Health ", Player.Health.CurrentVal) 대신 자본을 사용하지 않고 ("Health ", player.Health.CurrentVal) 사용하려고합니다. "Player"에서 동일한 스크립트에서 creaded 인스턴스를 참조), Visual은 "Player.Health는 객체의 인스턴스로 액세스 할 수 없습니다. 형식 이름 대신 "."으로 한정하십시오. 유니티도 "공용 플레이어 player = GameObject.FindGameObjectWithTag ("Player ")를 넣어야한다고 알려줍니다. GetComponent ();" Start() 메서드에서이 부분이 나를 어디로 유도하는지 알게 될 것입니다. – Nech

+0

Visual Studio에서 Start 메서드를 닫는 것으로 인식하지 못합니다. 대신 SaveLoad 클래스를 닫고 나머지 스크립트를 망칠 것입니다./ – Nech

+0

답변을 편집했습니다. 그 이유는 스크립트에서 다른 오류가 발생하고 통합이 변경 사항을 컴파일하지 않았기 때문입니다. – Bijan