什么是c#中缺少的引用异常?

时间:2017-03-13 10:14:34

标签: c# unity3d

我正在制作一个统一的游戏,我缺少参考异常。错误 不会出现在我的脚本编辑器中,所以我不知道这个错误是什么。 这是Gamemanager脚本:

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.Before之前没有出现错误。现在它就行了。我确信我接受了整个专业人员

1 个答案:

答案 0 :(得分:0)

我只是一个没有太多经验的初级开发人员,但你的代码看起来很乱。可能有更多的空引用错误,所以很可能你没有分配/保存预制某些东西或者你要使用的对象被销毁。检查[如何使用monobehaviour进行调试](https://unity3d.com/learn/tutorials/topics/scripting/monodevelops-debugger)。所以提出一些突破点,逐一审视。当断点激活时,您可以将鼠标悬停在变量上并检查它是否具有某个值。我强烈建议您在单独的行上进行任何分配:

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等。