C# 统一公共词典
我怎样才能使公共词典,我在其他脚本和对象中使用的是什么?我在Object和scriptA中有字典,我想用script/ObjectB中的命令向这个字典添加值 *例如,对象中的脚本:*C# 统一公共词典,c#,dictionary,unity3d,unityscript,C#,Dictionary,Unity3d,Unityscript,我怎样才能使公共词典,我在其他脚本和对象中使用的是什么?我在Object和scriptA中有字典,我想用script/ObjectB中的命令向这个字典添加值 *例如,对象中的脚本:* public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>(); 公共字典dictiono=newdictionary(); *例如,B对象中的B脚本:* public Dictio
public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>();
公共字典dictiono=newdictionary();
*例如,B对象中的B脚本:*
public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>();
A:
公共字典obstacleDictionary;
配子体采空区;
void Start()
{
obstacleDictionary=新字典();
pos=变换位置;
}
B:
vector3pos;
公共字符串开放端;
游戏对象玩家;
字符串posString;
无效开始()
{
player=GameObject.FindGameObjectWithTag(“player”);
pos=变换位置;
posString=pos.x+“”+pos.y;
player.GetComponent().obstacleDictionary.Add(posString,gameObject);//警告
//打印(player.GetComponent().obstacleDictionary.ContainsKey(“3_0”));
}
A.GetComponent我不知道什么对你不起作用,因为你没有提到什么有效或无效。下面是一个完整的示例:
您的ScriptA
与公共词典:
public class ScriptA : MonoBehaviour{
public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>();
}
确保用游戏对象的名称替换NameOfGameObjectScriptAIsAttachedTo
,ScriptA
已附加到
编辑:
查看您的项目并发现问题obstacleDictionary
在Start()
函数中初始化之前,正在从另一个脚本中使用
解决此问题的三种方法:
在你的PlayerMove.cs中
1。替换公共字典OBSTACLEDICTIONAL代码>
与
public Dictionary obstacleDictionary=new Dictionary()代码>
然后删除obstacleDictionary=newdictionary()从Start()
函数中选择code>
如果你遵循我最初的答案,它应该会起作用,但你没有。您所要做的就是在Start()
函数之外创建新实例
2。您可以移动obstacleDictionary=newdictionary()代码>从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);
}
}
}
public class PlayerMove:monobhavior
{
向量3位;
公众浮标速度;
内部公共静电门;
公共词典;
配子体采空区;
无效唤醒()
{
obstacleDictionary=新字典();
}
void Start()
{
pos=变换位置;
}
无效更新()
{
gob=GetObjectAt(转换位置);
如果(gob==null)inside=false;
else-inside=true;
印刷品(内部);
#区域控制
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
GameObject go=GetObjectAt(transform.position-newvector3(速度,0,0));
if(go==null | | go.GetComponent().openSide==right)
{
if(inside&&gob.GetComponent().openSide==“left”)inside=false;
/*if(内部&gob.CompareTag(“BigRed”)&go.CompareTag(“BigRed”){}
否则*/
位置x-=速度;
}
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
GameObject go=GetObjectAt(transform.position+newvector3(速度,0,0));
if(go==null | | go.GetComponent().openSide==left)
{
if(inside&&gob.GetComponent().openSide==“right”)inside=false;
位置x+=速度;
}
}
else if(Input.GetKeyDown(KeyCode.DownArrow))
{
GameObject go=GetObjectAt(transform.position-newvector3(0,speed,0));
if(go==null | | go.GetComponent().openSide==up)
{
if(inside&&gob.GetComponent().openSide==“down”)inside=false;
位置y-=速度;
}
}
else if(Input.GetKeyDown(KeyCode.UpArrow))
{
GameObject go=GetObjectAt(transform.position+newvector3(0,speed,0));
if(go==null | | go.GetComponent().openSide==down)
{
if(inside&&gob.GetComponent().openSide==“up”)inside=false;
位置y+=速度;
}
}
#端区
transform.position=pos;
if(内部&&gob.transform.position!=transform.position)
{
打印(“更改”);
ChangeObstacleDictionary(gob.transform.position,transform.position);
gob.transform.position=transform.position;
}
}
公共游戏对象GetObjectAt(矢量3位置)
{
字符串位置=位置x+“”+位置y;
if(obstacleDictionary.ContainsKey(pos)=true)
{
印刷品(非文字[pos]);
返回obstacleDictionary[pos];
}
否则返回null;
}
public void ChangeObstacleDictionary(Vector3 lastPosition,Vector3 newPos)
{
字符串lastPosString=lastPosition.x+“”+lastPosition.y;
字符串newPosString=newPos.x+“”+newPos.y;
//打印(“测试”+LastPoString+“”+NewPoString);
if(lastPosString!=newPosString)
{
obstacleDictionary.Remove(lastPosString);
obstacleDictionary.Add(newPosString,gob);
}
}
}
3。更改脚本的执行顺序,并使PlayerMove
先执行。
然后
那么,问题出在哪里?代码无法编译或出现任何运行时错误?是的,我的整个项目中都有bug,请您在问题中发布代码和问题,thanks@Valentin好的,我添加了代码,但当你下载unity project时,你更好地理解我的意思:)这不是工作,这是我的项目,请检查一下@Adam你试过我的解决方案了
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);
}
}
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);
}
}
}