so well here i am again
got some problems understanding as to why my data isn’t getting saved or working at all.
first of all i change change from a charactercreation scene. which i then set all the values. to my BasePlayer.
and it works all fine i can access the data after i changed scene when i use playerprefs but without it i cannot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CreateCharacterScript : MonoBehaviour {
private BasePlayer _newPlayer;
private BasicClass _newClass;
void Awake (){
}
// Use this for initialization
void Start () {
}
public void CreateCharacter (){
SceneManager.LoadScene("GameWorld",LoadSceneMode.Single);
_newPlayer = GetComponent<BasePlayer> ();
_newClass = GetComponent<BasicClass> ();
_newClass.Beginner ();
_newPlayer.PlayerName = "";
_newPlayer.PlayerLevel = 0;
_newPlayer.PlayerClass = _newClass.CharacterClassType;
_newPlayer.PlayerHealth = 50;
_newPlayer.PlayerMana = 50;
_newPlayer.PlayerStrength = 1;
_newPlayer.PlayerIntellect = 1;
_newPlayer.PlayerAgility = 1;
_newPlayer.PlayerLuck = 1;
_newPlayer.PlayerAttackSpeed = 1f;
_newPlayer.PlayerCastSpeed = 1f;
_newPlayer.PlayerSpeed = 200f;
_newPlayer.PlayerCurrentHealth = _newPlayer.PlayerHealth;
_newPlayer.PlayerMaxHealth = _newPlayer.PlayerHealth;
_newPlayer.PlayerMaxMana = _newPlayer.PlayerMana;
_newPlayer.PlayerCurrentMana = _newPlayer.PlayerCurrentMana;
StoreData ();
GameObject go = (GameObject)Instantiate(Resources.Load("Player"),new Vector3(502,20,-608),Quaternion.identity)as GameObject;
GameObject characterui = (GameObject)Instantiate (Resources.Load ("CharacterUI") as GameObject);
DontDestroyOnLoad (characterui.transform);
DontDestroyOnLoad (go.transform);
}
public void StoreData (){
Debug.Log ("Data was Stored");
PlayerPrefs.SetString ("name", _newPlayer.PlayerName);
PlayerPrefs.SetInt ("level", _newPlayer.PlayerLevel);
PlayerPrefs.SetFloat ("health", _newPlayer.PlayerHealth);
}
// Update is called once per frame
void Update () {
}
}
problem two is that when i try to access the data in this given script, it doesn’t give me the correct values out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour {
private BasePlayer player;
private BasicClass baseclass;
private BaseCharacter basecharacter;
public static PlayerUI _Instance{ get; private set;}
public Canvas newcanvas;
public Slider healthbar;
public Slider manabar;
public Text healthtext;
public Text manatext;
void Awake () {
if (_Instance == null) {
DontDestroyOnLoad(this);
_Instance = this;
}
else {
DestroyImmediate(this);
}
}
// Use this for initialization
void Start () {
player = GetComponent<BasePlayer> ();
healthbar.value = player.PlayerHealth;
Debug.Log ("UI Health: " + player.PlayerHealth);
}
// Update is called once per frame
void Update () {
}
}
third and last problem is that i set my PlayerScript basicly the one that the player object will hold. to have these values.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : BasePlayer {
void Awake(){
PlayerHealth = PlayerPrefs.GetFloat ("health",0);
PlayerLevel = PlayerPrefs.GetInt ("level",0);
PlayerName = PlayerPrefs.GetString ("name");
}
// Use this for initialization
void Start () {
Debug.Log ("NewHealth : " + PlayerHealth);
}
// Update is called once per frame
void Update () {
}
}
and for some reason when i try to access BasePlayer in my PlayerUI it says that Value = 0.
if someone has hints as to why i would love to hear them ![]()