提问人: 提问时间:6/18/2016 更新时间:6/18/2016 访问量:641
字典 - 对象引用未设置对象的实例
Dictionary - object reference not set an instance of an object
问:
我在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 ()
{
}
}
答: 暂无答案
评论
script