我不知道什么不适合你,因为你没有提到什么可行或不可行。这是一个完整的例子:
您的ScriptA 与公共词典:
public class ScriptA : MonoBehaviour{
public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>();
}
您可以通过ScriptB 访问它:
public class ScriptB : MonoBehaviour{
ScriptA scriptInstance = null;
void Start()
{
GameObject tempObj = GameObject.Find("NameOfGameObjectScriptAIsAttachedTo");
scriptInstance = tempObj.GetComponent<ScriptA>();
//Access dictio variable from ScriptA
scriptInstance.dictio.Add("aaa", gameObject);
}
}
确保将 NameOfGameObjectScriptAIsAttachedTo 替换为附加的游戏对象名称 ScriptA。
编辑:
查看了您的项目并发现了问题。 obstacleDictionary 在Start() 函数中初始化之前正在从另一个脚本中使用..
解决此问题的三种方法:
在您的 PlayerMove.cs 中,
1.替换public Dictionary<string, GameObject> obstacleDictionary;
与
public Dictionary<string, GameObject> obstacleDictionary = new Dictionary<string, GameObject>();。
然后从Start() 函数中删除obstacleDictionary = new Dictionary<string, GameObject>();。
如果您按照我最初的回答进行操作,但您没有这样做,它应该会起作用。您所要做的就是在 Start() 函数之外创建新实例。
2。您可以将obstacleDictionary = new Dictionary<string, GameObject>(); 从Start() 函数移动到Awake() 函数,它仍然可以工作。
您的新代码:
public class PlayerMove : MonoBehaviour
{
Vector3 pos;
public float speed;
public static bool inside;
public Dictionary<string, GameObject> obstacleDictionary;
GameObject gob;
void Awake()
{
obstacleDictionary = new Dictionary<string, GameObject>();
}
void Start()
{
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);
}
}
}
3.更改脚本的执行顺序,让PlayerMove 先执行。
然后