2016-06-18 7 views
1

私はどのようにパブリック辞書にすることができます、私はUnityの他のスクリプトとオブジェクトで何を使用していますか?私はオブジェクトとスクリプトの辞書を持っていますと私はスクリプト/オブジェクトBのコマンドでこの辞書に値を追加したい。例えばユニティの公開辞書

*オブジェクト内のスクリプト:*

public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>(); 

* Bのオブジェクトの例のBのスクリプトの場合:*

A:

public Dictionary<string, GameObject> obstacleDictionary; 

GameObject gob; 

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

} 

B:

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")); 
} 

A.GetComponent<AScript>().dictio.Add("aaa", gameObject); 

私を助けることができますか?ありがとう:)

+0

だから、問題は何ですか? – Valentin

+0

それは動作しません。 – Adam

+0

コードがコンパイルされないか、ランタイムエラーが発生しましたか? – Valentin

答えて

1

何が動作しているか動作していないと言わなかったので、何がうまく行かないのか分かりません。ここでは完全な例である:公共辞書と

あなたScriptA

public class ScriptA : MonoBehaviour{ 
    public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>(); 
} 

あなたがしてScriptBからアクセスすることができますゲームオブジェクトScriptAの名前でNameOfGameObjectScriptAIsAttachedToを置き換えてください

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); 
    } 
} 

されますに添付。

EDIT

あなたのプロジェクトを見て、問題を発見しました。あなたのPlayerMove.csで

.Replace public Dictionary<string, GameObject> obstacleDictionary;

obstacleDictionaryは、この問題を解決するために

3つの方法は... Start()機能で初期化される前に、別のスクリプトから使用されています

public Dictionary<string, GameObject> obstacleDictionary = new Dictionary<string, GameObject>();となります。

Start()機能からobstacleDictionary = new Dictionary<string, GameObject>();を削除します。

最初の回答に従っていても、そうしなかった場合はうまくいくはずです。 Start()関数の外に新しいインスタンスを作成するだけでした。

。あなたは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); 
     } 
    } 

} 

.Changeスクリプトの実行順序PlayerMoveが他の人の前に最初に実行します。 enter image description here

その後、

enter image description here

+0

これはうまくいかず、プロジェクトがありますので、https://1drv.ms/u/s!AsCxyl5DQ6LHnXch-oX8HW2OaIx9 – Adam

+0

@Adam 1分以内に私のソリューションを試しましたか?すばらしいです!さらに助けを求める努力をしてください。それを試してみてください! – Programmer

+0

私はあなたの前でそれを試したので。すべては私のプロジェクトにあります – Adam