如何在c#Unity 3d中解决NullReferenceException?

时间:2017-03-04 12:57:49

标签: c# unity3d scripting nullreferenceexception

我在下面的脚本的第38行有这个错误。我知道还有其他一些相同类型的问题。但没有一个对我有帮助。这个错误已经停止了我的项目。我想要摆脱它所以,如果有人能帮助我,我会非常感谢。这就是我的代码

using System.Collections.Generic;
using UnityEngine;


public class LootChest : MonoBehaviour
{
public int MaxItems;
int ItemCount;
List<Item> Items = new List<Item>();
public GameManager GameManager;
bool Selected;
public float Distance;
public Color HoverColour;
public Color ClickColour;
Color DefaultColor;
// Use this for initialization
void Start()
{
    ItemCount = Random.Range(0, MaxItems);
    for (int i = 0; i < ItemCount; i++)
    {
        int r = Random.Range(0, GameManager.Instance.AllItems.Count - 1);
        Items.Add(GameManager.Instance.AllItems[r]);
    }

    DefaultColor = GetComponent<Renderer>().material.color;
}

// Update is called once per frame
void Update()
{


}

void OnMouseOver()
{
    if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) > Distance);
    GetComponent<Renderer>().material.color = HoverColour;
}

void OnMouseExit()
{
    if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) < Distance) ;
    GetComponent<Renderer>().material.color = DefaultColor;
}
void OnMouseDown()
{

}
void OnMouseUp()
{

}
}

错误在线

   if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) < Distance) ;

这个错误不会出现在脚本编辑器中(Visual Studio 2017)。我认为这是一个基于统一的错误而不是基于脚本的错误。 This is the image of unity with the cube object

这是GameManager脚本

using UnityEngine;
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;
  public LootChest SelectedChest;

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<asd>().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<asd> ().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;
 }

0 个答案:

没有答案