작동하지 않는 것이 무엇인지 언급하지 않았기 때문에 어떤 것이 작동하지 않는지 알 수 없습니다.
귀하의 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);
}
}
ScriptA
는 게임 오브젝트의 이름으로 NameOfGameObjectScriptAIsAttachedTo
을 교체해야 부착.
편집 :
은 당신의 프로젝트를 쳐다 보면서 문제를 발견했다.
.Replace public Dictionary<string, GameObject> obstacleDictionary;
당신의 PlayerMove.cs에서
,
1 : obstacleDictionary
이 있습니다 .. Start()
기능에서 초기화되기 전에 다른 스크립트에서
세이 문제를 해결하는 방법을 사용하고 있습니다
public Dictionary<string, GameObject> obstacleDictionary = new Dictionary<string, GameObject>();
.
Start()
기능에서 obstacleDictionary = new Dictionary<string, GameObject>();
을 제거하십시오.
처음 대답을 따라 갔지만 제대로하지 않으면 작동했을 것입니다. Start()
함수 외부에 새 인스턴스를 작성하기 만하면됩니다.
2 우선은이 Awake()
함수에 Start()
함수에서 obstacleDictionary = new Dictionary<string, GameObject>();
를 이동할 수 있으며, 그것은 여전히 작동합니다.
새 코드 :
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 .Change 실행 순서 스크립트의 및 확인 PlayerMove
다른 사람보다 먼저 실행합니다.
다음
그래서, 문제는 무엇인가? – Valentin
그것은 작동하지 않습니다. – Adam
코드가 컴파일되지 않거나 런타임 오류가 있습니까? – Valentin