【发布时间】:2019-03-29 13:30:10
【问题描述】:
[Picture]我正在开发一款游戏并尝试制作一个保存和加载按钮。我相信我的编码是正确的,但我的问题是找不到我想要访问的播放器。我怀疑这与我的编码方式有关。
由于有多个级别,我使用了以下代码: GameObject player = GameObject.FindWithTag("Player");尝试在场景中找到给定的玩家,但我认为它不起作用。
我没有将播放器设置为公共变量,因为它在每个场景中都会发生变化。
例如,我尝试使用以下代码行访问玩家当前的健康状况,这些代码保存在名为 Gamecontrol.cs 的脚本中的 GameController 空游戏对象中:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour {
public static GameControl controller;
int shooterHealth;
int shooterScore;
//public GameObject player;
//public GameObject gun;
int level;
// Use this for initialization
void Awake() {
GameObject player = GameObject.FindWithTag("Player");
shooterHealth = player.GetComponent<PlayerHealth>().currentHealth;
shooterScore = player.GetComponent<Gun>().score;
level = player.GetComponent<PlayerHealth>().level;
if(controller == null) {
DontDestroyOnLoad(gameObject);
controller = this;
}
else if(controller != this) {
Destroy(gameObject);
}
}
public void SaveGame() {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/shooter.dat");
PlayerData data = new PlayerData();
data.shooterHealth = shooterHealth;
data.shooterScore = shooterScore;
data.level = level;
bf.Serialize(file, data);
file.Close();
}
public void LoadGame() {
if(File.Exists(Application.persistentDataPath + "/shooter.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/shooter.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
GameObject player = GameObject.FindWithTag("Player");
player.GetComponent<PlayerHealth>().currentHealth = data.shooterHealth;
player.GetComponent<Gun>().score = data.shooterScore;
level = player.GetComponent<PlayerHealth>().level;
SceneManager.LoadScene(level);
}
}
}
[Serializable]
class PlayerData
{
public int shooterHealth;
public int shooterScore;
public int level;
}
我得到的错误是:
NullReferenceException:对象引用未设置为对象的实例 GameControl.Awake () (在 Assets/GameControl.cs:20)
有人知道解决这个问题的方法吗?提前谢谢你:)
【问题讨论】:
-
您确定播放器标记正确吗?
-
是的,我敢肯定,我什至尝试将标签更改为不同的标签,它说同样的错误:(
-
这很奇怪。你能分享完整的脚本,也许你在代码的其他部分做错了
-
玩家也在场景中活跃吗?如果没有,那你就找不到了。
-
我更新了一张图片:)