C#中缺少什么引用异常? [英] What is missing reference exception in c#?
问题描述
我正在用统一3d制作游戏,但缺少参考异常.错误 不会出现在我的脚本编辑器中,所以我不知道此错误与什么有关. 这是Gamemanager脚本:
I am making a game in unity 3d and I have missing reference exception.The error don't appear in my script editor so I don't know what is this error related to. Here is the Gamemanager script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gamemanager : MonoBehaviour
{
public List<Character> Characters = new List<Character>();
public List <Item> AllItems = new List<Item> ();
bool ShowCharWheel;
public int SelectedCharacter;
int LastCharacter;
public static Gamemanager Instance;
public bool CanShowSwitch = true;
public Character CurrentCharacter;
void Awake()
{
foreach (Character c in Characters)
{
c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
c.Instance.GetComponent<PlayerController> ().LocalCharacter = c;
}
ChangeCharacterStart(Characters[PlayerPrefs.GetInt("SelectedChar")]);
}
// Use this for initialization
void Start()
{
}
void ChangeCharacterStart(Character c)
{
LastCharacter = SelectedCharacter;
SelectedCharacter = Characters.IndexOf(c);
CurrentCharacter = c;
Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
Camera.main.GetComponent<SmoothFollow>().target = Characters[SelectedCharacter].Instance.transform;
PlayerPrefs.SetInt("SelectedChar", SelectedCharacter);
}
// Update is called once per frame
void Update()
{
if (CanShowSwitch) {
if (Input.GetKey (KeyCode.C)) {
ShowCharWheel = true;
} else if (Input.GetKey (KeyCode.V)) {
ShowCharWheel = false;
}
}
}
void ChangeCharacter(Character c)
{
c.Instance.GetComponent<AI> ().DoneHome = false;
if (Vector3.Distance (Characters [SelectedCharacter].Instance.transform.position, c.Instance.transform.position) > 10) {
sequencemanager.Instance.StartCoroutine ("DoCharSwitch", c);
CanShowSwitch = false;
LastCharacter = SelectedCharacter;
SelectedCharacter = Characters.IndexOf (c);
CurrentCharacter = c;
Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
} else {
LastCharacter = SelectedCharacter;
SelectedCharacter = Characters.IndexOf(c);
CurrentCharacter = c;
Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
Camera.main.GetComponent<SmoothFollow> ().target = Characters [SelectedCharacter].Instance.transform;
}
}
void OnGUI()
{
if (ShowCharWheel)
{
GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 256, 64, 208), GUIContent.none, "box");
foreach (Character c in Characters)
{
if (GUILayout.Button(c.Icon, GUILayout.Width(64), GUILayout.Height(64)))
{
ChangeCharacterStart(c);
}
}
GUILayout.EndArea();
}
}
}
[System.Serializable]
public class Character
{
public string Name;
public Texture2D Icon;
public GameObject PlayerPrefab;
public GameObject Instance;
public Transform HomeSpawn;
}
[System.Serializable]
public class Item
{
public string Name;
public Texture2D Icon;
public ItemInstance InstancePrefab;
}
错误在线
c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
这是我的编辑器图像,带有错误.主要问题是,当我启动游戏时,它破坏了游戏管理器脚本. 还有一件重要的事情:我迁移了这个项目.我不得不重新安装Windows.
This is my editor image with error.The main problem is when I start the game it destroy the gamemanager script. And another important thing.I migrated this project.I had to reinstall windows.Before that the error didn't appeared.And now it do.And I am sure I took the whole pro
推荐答案
我只是一个初级开发人员,没有太多经验,但是您的代码看起来很凌乱.可能存在更多的null refs错误,因此很可能您没有分配/保存预制件或要使用的对象被破坏.检查[如何使用monobehaviour进行调试]( https://unity3d.com/learn/tutorials/topics/scripting/monodevelops-debugger ).因此,请设置一些断点,然后逐一进行.激活断点后,您可以将鼠标悬停在变量上,并检查其是否具有某些值.我强烈建议您像这样在单独的行中进行任何分配:
I just a junior developer without much of experience but your code looks messy. May be there is more null refs errors so most probably you did not assign/ save to prefab something or an object that you want to use is destroyed. Check [how to debug with monobehaviour] (https://unity3d.com/learn/tutorials/topics/scripting/monodevelops-debugger). So put some break point, and go through them one by one. When break point activated you can hover over variable and check if it has some value. I strongly suggest you make any assignment on separate line like this:
c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
应该是这样的:
GameObject newGameObject = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
c.Instance = newGameObject;
在这种情况下,您可以检查是否确实有newGameObject等.
In that case you can check if you really have newGameObject and so on.
这篇关于C#中缺少什么引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!