accessing values in another class with a non mono,static class

so i ran into some trouble with how i should go about updating my UI by getting a Player’s health and dmg and so on.

also the player class isnt attached to any gameobjects

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasePlayer {

    public BasePlayer(){
      
    }

    private string playerName;
    private string playerDescription;

    private float playerHealth;
    private float playerMana;

    private float playerCurrentHealth;
    private float playerCurrentMana;

    private float playerMaxHealth;
    private float playerMaxMana;

    private BaseCharacterClass.ClassType playerClass;
    private int playerLevel;

    private int playerStrength;
    private int playerIntellect;
    private int playerAgility;
    private int playerLuck;
    private float playerAttackSpeed;
    private float playerCastSpeed;
    private float playerSpeed;


    public string PlayerName { get { return playerName; } set { playerName = value; } }
    public string PlayerDescription { get { return playerDescription; } set { playerDescription = value; } }

    public float PlayerHealth { get { return playerHealth; } set { playerHealth = value; } }
    public float PlayerMana { get { return playerMana; } set { playerMana = value; } }

    public float PlayerCurrentHealth { get { return playerCurrentHealth; } set { playerCurrentHealth = value; } }
    public float PlayerCurrentMana { get { return playerCurrentMana; } set { playerCurrentMana = value; } }

    public float PlayerMaxHealth { get { return playerMaxHealth; } set { playerMaxHealth = value; } }
    public float PlayerMaxMana { get { return playerMaxMana; } set { playerMaxMana = value; } }

    public BaseCharacterClass.ClassType PlayerClass { get { return playerClass; } set { playerClass = value; } }
    public int PlayerLevel { get { return playerLevel; } set { playerLevel = value; } }

    public int PlayerStrength { get { return playerStrength; } set { playerStrength = value; } }
    public int PlayerIntellect { get { return playerIntellect; } set { playerIntellect = value; } }
    public int PlayerAgility { get { return playerAgility; } set { playerAgility = value; } }
    public int PlayerLuck { get { return playerLuck; } set { playerLuck = value; } }

    public float PlayerAttackSpeed { get { return playerAttackSpeed; } set { playerAttackSpeed = value; } }
    public float PlayerCastSpeed { get { return playerCastSpeed; } set { playerCastSpeed = value; } }
    public float PlayerSpeed { get { return playerSpeed; } set { playerSpeed = value; } }

}

This class doesn’t inherit from Monobehaviour neither does the BasePlayer which holds all information about stats and name for player,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : BasePlayer {



    public Player()
    {
      



        GameObject go = (GameObject)MonoBehaviour.Instantiate(Resources.Load("Player"),new Vector3(502,20,-608),Quaternion.identity)as GameObject;
        GameObject gamescene = (GameObject)MonoBehaviour.Instantiate(Resources.Load("CharacterUI")) as GameObject;
        MonoBehaviour.DontDestroyOnLoad (go.transform);


        //GameObject go = MonoBehaviour.Instantiate(Resources.Load("MyPrefab")) as GameObject;
        LoadInformation.LoadAllInformation ();

        PlayerName = GameInformation.playerName;
        PlayerLevel = GameInformation.playerLevel;
        PlayerHealth = GameInformation.playerHealth;
        PlayerMaxHealth =    GameInformation.playerMaxHealth;
        PlayerCurrentHealth =    GameInformation.playerCurrentHealth;
        PlayerMana =    GameInformation.playerMana;
        PlayerStrength =    GameInformation.playerStrength;
        PlayerIntellect =    GameInformation.playerIntellect;
        PlayerAgility =    GameInformation.playerAgility;
        PlayerLuck =     GameInformation.playerLuck;
        PlayerAttackSpeed =    GameInformation.playerAttackSpeed;
        PlayerCastSpeed =    GameInformation.playerCastSpeed;
        PlayerSpeed =     GameInformation.playerSpeed;






        Debug.Log ("PlayerHealth " + PlayerCurrentHealth.ToString());

   }

    public void DealDamage(float dmgvalue){
        PlayerCurrentHealth -= dmgvalue;
        updatehealth ();
    }


    public void updatehealth(){
        Debug.Log ("Event was Fired");
        EventManager.updatehealthevent += CalculateHealth;

    }

    public float CalculateHealth(){
        EventManager.updatehealthevent -= CalculateHealth;
        return PlayerCurrentHealth / PlayerMaxHealth;
    }




}

this is my PlayerUI script that is currently attached to a Canvas

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerUI : MonoBehaviour {


    public static PlayerUI _Instance { get; private set;}

    public Canvas canvasui;
    public Slider healthbar;
    public Slider manabar;
    public Text healthtext;
    public Text manatext;





    // Use this for initialization
    void Awake () {

        if (_Instance == null) {
            DontDestroyOnLoad(this);
            _Instance = this;
        }
        else {
          
            DestroyImmediate(this);
        }


    }

    void Start(){


      
    }


  
    // Update is called once per frame
    void Update () {
       i want to somehow access my playerdata here so i can update the GUI and display correctly.
    }

    void FixedUpdate(){
      
    }
}

To answer your main question, you can simply say Player myPlayer = new Player();
And if you need to, you can reference it in a singleton or as a static object.

BUT

Let’s take a step back and look at this monstrosity:

        PlayerName = GameInformation.playerName;
        PlayerLevel = GameInformation.playerLevel;
        PlayerHealth = GameInformation.playerHealth;
        PlayerMaxHealth =    GameInformation.playerMaxHealth;
        PlayerCurrentHealth =    GameInformation.playerCurrentHealth;
        PlayerMana =    GameInformation.playerMana;
        PlayerStrength =    GameInformation.playerStrength;
        PlayerIntellect =    GameInformation.playerIntellect;
        PlayerAgility =    GameInformation.playerAgility;
        PlayerLuck =     GameInformation.playerLuck;
        PlayerAttackSpeed =    GameInformation.playerAttackSpeed;
        PlayerCastSpeed =    GameInformation.playerCastSpeed;
        PlayerSpeed =     GameInformation.playerSpeed;

If you’ve got long blocks of tedious code like this, then your data structure design needs an overhaul. Especially because you’re likely to be adding to this list of data about the player as development continues, and everything you add, you’re adding to at least four (and I suspect five or more) different places in the code.

I’d have to see what’s in GameInformation to know the best way to resolve this, but I suspect that you should probably have GameInformation have a Player object (that you simply either reference or clone), rather than every individual static variable that later gets copied over to the object. Bonus: if GameInformation has a static Player object, this will give you an easy answer to your original question!

Another solution might be to take advantage of serialization (JsonUtility is wonderful for this sort of thing), which would also allow you to easily save game progress, assuming these player attributes can change over the course of the game.

only using it for saving information once. and a place to hold information.

also i cannot use New keyword as that would make a new player which would instantiate it again?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameInformation : MonoBehaviour {

    void Awake()
    {
        DontDestroyOnLoad(transform.gameObject);
    }

    public static string playerName;
    public static string playerDescription;

    public static float playerHealth;
    public static float playerMana;

    public static float playerCurrentHealth;
    public static float playerCurrentMana;

    public static float playerMaxHealth;
    public static float playerMaxMana;

    public static BaseCharacterClass.ClassType playerClass;
    public static int playerLevel;

    public static int playerStrength;
    public static int playerIntellect;
    public static int playerAgility;
    public static int playerLuck;
    public static float playerAttackSpeed;
    public static float playerCastSpeed;
    public static float playerSpeed;



}

i create player here. so i can’t really use new keyword again?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CreateNewCharacter : MonoBehaviour {

    private PlayerScript newPlayer;
    private PlayerUI newui;
    private BasicClass newClass;
    private Scene scene;
    private string playerName;
    private bool isBeginner;

    public Button charactercreate;
    // Use this for initialization

    void Start () {



    }
   
    // Update is called once per frame
    void Update () {
       
    }


    public void CreateCharacter()
    {
        SceneManager.LoadScene("GameScene",LoadSceneMode.Single);

        //Debug.Log (SceneManager.SetActiveScene (SceneManager.GetSceneByName ("GameScene")));


            newPlayer = new PlayerScript();
            newClass = new BasicClass().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;
           

            StoreNewPlayerInfo();
            SaveInformation.SaveAllInformation();
            Debug.Log("Name : " + newPlayer.PlayerName);
            Debug.Log("Level : " + newPlayer.PlayerLevel);
            Debug.Log("Class : " + newPlayer.PlayerClass);
            Debug.Log("Health : " + newPlayer.PlayerHealth);
            Debug.Log("Mana : " + newPlayer.PlayerMana);
            Debug.Log("Str : " + newPlayer.PlayerStrength);
            Debug.Log("Int : " + newPlayer.PlayerIntellect);
            Debug.Log("Agi : " + newPlayer.PlayerAgility);
            Debug.Log("Luck : " + newPlayer.PlayerLuck);

           
       


   
        //newui = GameObject.Find ("CharacterUI").GetComponent<PlayerUI> ();
    }

    private void StoreNewPlayerInfo()
    {
        GameInformation.playerName = newPlayer.PlayerName;
        GameInformation.playerLevel = newPlayer.PlayerLevel;
        GameInformation.playerHealth = newPlayer.PlayerHealth;
        GameInformation.playerMaxHealth = newPlayer.PlayerHealth;
        GameInformation.playerCurrentHealth = newPlayer.PlayerCurrentHealth;
        GameInformation.playerMana = newPlayer.PlayerMana;
        GameInformation.playerStrength = newPlayer.PlayerStrength;
        GameInformation.playerIntellect = newPlayer.PlayerIntellect;
        GameInformation.playerAgility = newPlayer.PlayerAgility;
        GameInformation.playerLuck = newPlayer.PlayerLuck;
        GameInformation.playerAttackSpeed = newPlayer.PlayerAttackSpeed;
        GameInformation.playerCastSpeed = newPlayer.PlayerCastSpeed;
        GameInformation.playerSpeed = newPlayer.PlayerSpeed;
       
    }
   
}

Okay, so yes, my recommendation is to wipe out all those static members from the GameInformation class, and replace it with a “public static Player player;” line. Then you can simply remove that block of code I quoted from the Player class, and anytime you need this data, replace

float someHealth = GameInformation.playerHealth;

with

float someHealth = GameInformation.player.PlayerHealth;

Including what I’m guessing is in LoadInformation.LoadAllInformation() - it can put that information directly into the Player class the same way. And you can use GameInformation.player to answer your original question of how to access it without creating a new instance of it.

[quote=StarManta, post
: 3003327, member: 1195]
Okay, so yes, my recommendation is to wipe out all those static members from the GameInformation class, and replace it with a “public static Player player;” line. Then you can simply remove that block of code I quoted from the Player class, and anytime you need this data, replace

float someHealth = GameInformation.playerHealth;

with

float someHealth = GameInformation.player.PlayerHealth;

And you can use GameInformation.player to answer your original question of how to access it without creating a new instance of it.
[/quote]

haha fuck me :smile: i completly overlooked it imo :smile: thanks man most of the times i overlook simple solution imo,

mhm that Public Static PlayerScript player what should it return?

bah it did not work sadly. it now requires a instance of

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadInformation : MonoBehaviour {
 
    public static void LoadAllInformation()
    {
     
        GameInformation.player.PlayerName = PlayerPrefs.GetString("PLAYERNAME");
        GameInformation.player.PlayerHealth = PlayerPrefs.GetFloat("PLAYERHEALTH");
        GameInformation.player.PlayerMaxHealth = PlayerPrefs.GetFloat ("PLAYERMAXHEALTH");
        GameInformation.player.PlayerCurrentHealth = PlayerPrefs.GetFloat ("PLAYERCURRENTHEALTH");
        GameInformation.player.PlayerMana = PlayerPrefs.GetFloat("PLAYERMANA");
        GameInformation.player.PlayerStrength = PlayerPrefs.GetInt("PLAYERSTRENGTH");
        GameInformation.player.PlayerIntellect =  PlayerPrefs.GetInt("PLAYERINTELLECT");
        GameInformation.player.PlayerAgility = PlayerPrefs.GetInt("PLAYERAGILITY");
        GameInformation.player.PlayerAttackSpeed = PlayerPrefs.GetFloat("PLAYERATTACKSPEED");
        GameInformation.player.PlayerCastSpeed = PlayerPrefs.GetFloat("PLAYERCASTSPEED");
        GameInformation.player.PlayerSpeed = PlayerPrefs.GetFloat("PLAYERSPEED");
        Debug.Log("Load SucessFul");
    }
 
}

and before gameinformation did not do that.

Note this is where i create a copy of PlayerScript and instantiate it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CreateNewCharacter : MonoBehaviour {

    private PlayerScript newPlayer;
    private PlayerUI newui;
    private BasicClass newClass;
    private Scene scene;
    private string playerName;
    private bool isBeginner;

    public Button charactercreate;
    // Use this for initialization

    void Start () {
      
        //GameObject gameinformation = (GameObject)Instantiate(Resources.Load("GameInformation")) as GameObject;
        //CreateCharacter();

    }
  
    // Update is called once per frame
    void Update () {
      
    }


    public void CreateCharacter()
    {
        SceneManager.LoadScene("GameScene",LoadSceneMode.Single);

        //Debug.Log (SceneManager.SetActiveScene (SceneManager.GetSceneByName ("GameScene")));

            newPlayer = new PlayerScript();
            newClass = new BasicClass().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;
          

            StoreNewPlayerInfo();
            SaveInformation.SaveAllInformation();
        /*
            Debug.Log("Name : " + newPlayer.PlayerName);
            Debug.Log("Level : " + newPlayer.PlayerLevel);
            Debug.Log("Class : " + newPlayer.PlayerClass);
            Debug.Log("Health : " + newPlayer.PlayerHealth);
            Debug.Log("Mana : " + newPlayer.PlayerMana);
            Debug.Log("Str : " + newPlayer.PlayerStrength);
            Debug.Log("Int : " + newPlayer.PlayerIntellect);
            Debug.Log("Agi : " + newPlayer.PlayerAgility);
            Debug.Log("Luck : " + newPlayer.PlayerLuck);

        */  
      


  
        //newui = GameObject.Find ("CharacterUI").GetComponent<PlayerUI> ();
    }

    private void StoreNewPlayerInfo()
    {
        GameInformation.player.PlayerName = newPlayer.PlayerName;
        GameInformation.player.PlayerLevel = newPlayer.PlayerLevel;
        GameInformation.player.PlayerHealth = newPlayer.PlayerHealth;
        GameInformation.player.PlayerMaxHealth = newPlayer.PlayerHealth;
        GameInformation.player.PlayerCurrentHealth = newPlayer.PlayerCurrentHealth;
        GameInformation.player.PlayerMana = newPlayer.PlayerMana;
        GameInformation.player.PlayerStrength = newPlayer.PlayerStrength;
        GameInformation.player.PlayerIntellect = newPlayer.PlayerIntellect;
        GameInformation.player.PlayerAgility = newPlayer.PlayerAgility;
        GameInformation.player.PlayerLuck = newPlayer.PlayerLuck;
        GameInformation.player.PlayerAttackSpeed = newPlayer.PlayerAttackSpeed;
        GameInformation.player.PlayerCastSpeed = newPlayer.PlayerCastSpeed;
        GameInformation.player.PlayerSpeed = newPlayer.PlayerSpeed;
      
    }
  
}

for some reason this doesn’t work and i dont want to recreate a new playerscript as that would reset it all. and also mess up the UI

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameInformation : MonoBehaviour {

    public static GameInformation instance = null;
    public static PlayerScript player;


    void Awake()
    {
       
        if (instance == null) {
            instance = this;
        }

        else if (instance != this) {
            Destroy (gameObject);
        }

        DontDestroyOnLoad (gameObject);


    }

i also have followed the same tutorial as simonlvschal, and i am in the same situation.
could you explain why they are creating a base player, a a game information to store everything (that is static) and also how to and why it is better to use your solution ? ^^ thanks you !

i would really appreciate some hitns @StarManta if that can help you i got all my file on github at https://github.com/mattthhdp/NoMana/tree/master/Assets/Scripts
thank again