如何从对象的实例访问公共变量?

问题描述 投票:0回答:1

我目前有一个GameMaster对象,该对象用于通过过渡场景保存统计信息。我要存储的字段之一是playerMaxHealth值。我试图在每个场景的开头将此值加载到playerStats.MaxHealth值中的Player对象中。此时,我的GameMaster实例工作正常,并且各个场景之间都保存了值,但是我无法从新加载的Player对象访问playerMaxHealth值。我已经尝试了许多事情,包括此站点上的解决方案,但是我永远无法访问该对象。我以两种脚本的形式包含了当前尝试的代码,以及当前的错误消息(随着我尝试的每种方法的不同,它都发生了变化)。访问此值并将其加载到Player playerStats.MaxHealth值的正确方法是什么?

游戏大师:

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

    public class GameMaster : MonoBehaviour
    {
        public static GameMaster Instance;    

        public static int playerLevel = 1;
        public static int playerExp = 0;
        public static int expTilNextLevel = 1000;
        public static int playerMaxHealth = 100;


        void Awake () {
            if (Instance == null) {
                DontDestroyOnLoad(gameObject);
                Instance = this;
            }
            else if (Instance != this){
                Destroy (gameObject);
            }
        }

        void Update() {
            if(Input.GetKeyDown(KeyCode.L)){
                AddExp();
            }

            if(Input.GetKeyDown(KeyCode.H)){
                //Player.DamagePlayer(10);
            }
        }

        public static void KillPlayer (Player player) {
            Destroy (player.gameObject);
        }

        public static void KillEnemy (Enemy enemy) {
            Destroy (enemy.gameObject);
            AddExp();
        }

        private static void AddExp () {
            playerExp += 100;
            if (playerExp >= expTilNextLevel) {
                playerLevel++;
                playerExp = playerExp - expTilNextLevel;
                expTilNextLevel = (int)((float)expTilNextLevel * 1.5f);
            } 
            Debug.Log("Player Exp: " + playerExp + "/" + expTilNextLevel + "   Player Lvl: " + playerLevel);
        }

    }

PLAYER:



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

    public class Player : MonoBehaviour
    {

        [System.Serializable]
            public class PlayerStats {
            public int MaxHealth = 100;
            public int Health = 100;
            public int Exp = 0;
            public int Strength = 1;
        }

        public PlayerStats playerStats = new PlayerStats();


        void Start() {
            GameObject gameMaster = GameObject.FindWithTag("GameMaster") as GameObject;
            playerStats.MaxHealth = gameMaster.playerMaxHealth;
            Debug.Log("Player Max Health: ");
        }


        public void DamagePlayer(int damage){
            playerStats.Health -= damage;
            if (playerStats.Health <= 0) {
                GameMaster.KillPlayer(this);
                Debug.Log ("YOU DIED");
            }
        }

    }

错误消息:'GameObject'不包含'playerMaxHealth'的定义,并且找不到可访问的扩展方法'playerMaxHealth'接受类型为'GameObject'的第一个参数(您是否缺少using指令或程序集引用?)

c# unity3d instance gameobject
1个回答
0
投票

如果GameMaster是连接到GameObject的组件,则playerMaxHealth是该组件的属性,而不是GameObject

您要做的是获取组件本身,以便您可以访问成员变量。

例如:

GameMaster gameMasterComponent = gameMaster.GetComponent<GameMaster>();

playerStats.MaxHealth = gameMasterComponent.playerMaxHealth;

或者,由于您将GameMaster视为单例,并且playerMaxHealth是静态的,因此以下任何一种方法也应该起作用:

// as singleton 
playerStats.MaxHealth = GameMaster.Instance.playerMaxHealth;

// as static property
playerStats.MaxHealth = GameMaster.playerMaxHealth;
© www.soinside.com 2019 - 2024. All rights reserved.