如何从对象的实例访问 public 变量?
How can I access a public variable from an Instance of an object?
我目前有一个 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);
}
}
玩家:
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' 的定义并且无法找到接受类型 'GameObject' 的第一个参数的可访问扩展方法 'playerMaxHealth'(是您缺少 using 指令或程序集引用?)
如果 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;
我目前有一个 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);
}
}
玩家:
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' 的定义并且无法找到接受类型 'GameObject' 的第一个参数的可访问扩展方法 'playerMaxHealth'(是您缺少 using 指令或程序集引用?)
如果 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;