字典 - 对象引用未设置对象的实例

Dictionary - object reference not set an instance of an object

提问人: 提问时间:6/18/2016 更新时间:6/18/2016 访问量:641

问:

我在Unity2D中制作,但我遇到了一个问题。我有一个对象(它有 ObstacleMove 脚本),我想在场景中多次使用,并且我想将这个对象的每个实例保存在字典中(字典在其他脚本和其他对象中)。我在 ObstaceleScript 中使用:

player.GetComponent<PlayerMove>().obstacleDictionary.Add(posString, gameObject);,

一切正常,但是当我在 ObstacleMove 脚本中使用多个对象时,控制台写入错误:对象引用未设置对象的实例

那么我可以使用什么来代替Add(posString, ___); 中的游戏对象?

PS:PlayerMove是带有字典的脚本,player是使用此脚本的对象。

附言 2:Dictionary<string, GameObject> obstacleDictionary;

玩家移动

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerMove : MonoBehaviour
{

Vector3 pos;
public float speed;
public static bool inside;
public Dictionary<string, GameObject> obstacleDictionary;

GameObject gob;

void Start()
{
    obstacleDictionary = new Dictionary<string, GameObject>();
    pos = transform.position;

}

void Update()
{
    gob = GetObjectAt(transform.position);
    if (gob == null) inside = false;
    else inside = true;
    print(inside);


    #region Control
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        GameObject go = GetObjectAt(transform.position - new Vector3(speed, 0, 0));
        if (go == null || go.GetComponent<ObstacleMove>().openSide == "right")
        {
            if (inside && gob.GetComponent<ObstacleMove>().openSide == "left") inside = false;
            /*if(inside && gob.CompareTag("BigRed") && go.CompareTag("BigRed")) { }
            else */pos.x -= speed;
        }
    }

    else if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        GameObject go = GetObjectAt(transform.position + new Vector3(speed, 0, 0));
        if (go == null || go.GetComponent<ObstacleMove>().openSide == "left")
        {
            if (inside && gob.GetComponent<ObstacleMove>().openSide == "right") inside = false;
            pos.x += speed;
        }
    }

    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        GameObject go = GetObjectAt(transform.position - new Vector3(0, speed, 0));
        if (go == null || go.GetComponent<ObstacleMove>().openSide == "up")
        {
            if (inside && gob.GetComponent<ObstacleMove>().openSide == "down") inside = false;
            pos.y -= speed;
        }
    }

    else if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        GameObject go = GetObjectAt(transform.position + new Vector3(0, speed, 0));
        if (go == null || go.GetComponent<ObstacleMove>().openSide == "down")
        {
            if (inside && gob.GetComponent<ObstacleMove>().openSide == "up") inside = false;
            pos.y += speed;
        }
    }
    #endregion

    transform.position = pos;

    if (inside && gob.transform.position != transform.position)
    {
        print("change");
        ChangeObstacleDictionary(gob.transform.position, transform.position);
        gob.transform.position = transform.position;

    }
}

public GameObject GetObjectAt(Vector3 position)
{
    string pos = position.x + "_" + position.y;
    if (obstacleDictionary.ContainsKey(pos) == true)
    {
        print(obstacleDictionary[pos]);
        return obstacleDictionary[pos];
    }

    else return null;
}

public void ChangeObstacleDictionary(Vector3 lastPosition, Vector3 newPos)
{

    string lastPosString = lastPosition.x + "_" + lastPosition.y;
    string newPosString = newPos.x + "_" + newPos.y;

    //print("test" + lastPosString + " " + newPosString);
    if (lastPosString != newPosString)
    {
        obstacleDictionary.Remove(lastPosString);
        obstacleDictionary.Add(newPosString, gob);
    }
}

}

障碍移动:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObstacleMove : MonoBehaviour {

Vector3 pos;
public string openSide;
GameObject player;
string posString;

void Start ()
{
    player = GameObject.FindGameObjectWithTag("Player");
    pos = transform.position;
    posString = pos.x + "_" + pos.y;


    player.GetComponent<PlayerMove>().obstacleDictionary.Add(posString, gameObject); //warning

    //print(player.GetComponent<PlayerMove>().obstacleDictionary.ContainsKey("3_0"));
}


void Update ()
{


}
}
对象 unity-game-engine nullreferenceexception unityscript

评论

0赞 6/18/2016
删除答案的答案:但是不,它只是字典中所有对象之一,它不会抛出错误,但它不能满足我想要的:)
0赞 Blue Eyed Behemoth 6/18/2016
我意识到我误读了你的问题,哈哈
0赞 6/18/2016
但是谢谢,你能帮我吗?:)
0赞 Blue Eyed Behemoth 6/18/2016
这是用什么语言写的?那是 C# 吗?script
0赞 6/18/2016
是的,我使用 C# 语言

答: 暂无答案