1

how I can make public Dictionary, what i use in other script and object in Unity? I have Dictionary in Object and script A and i want add values to this Dictionary with command in script/object B.

*For example A script in A object: *

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

*For example B script in B object: *

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

Can you help me? Thanks :)

Ruzihm
  • 19,749
  • 5
  • 36
  • 48

1 Answers1

2

I can't tell what doesn't work for you because you didn't mention what works or doesn't work. Here is a complete example:

Your ScriptA with public dictionary:

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

You can access it from ScriptB with:

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

Make sure to replace NameOfGameObjectScriptAIsAttachedTo with the name of GameObject ScriptA is attached to.

EDIT:

Looked at your Project and found the problem. obstacleDictionary is being used from another script before being initialized in the Start() function..

Three ways to fix this:

In your PlayerMove.cs,

1.Replace public Dictionary<string, GameObject> obstacleDictionary;

with

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

then remove obstacleDictionary = new Dictionary<string, GameObject>(); from the Start() function.

It should have worked if you followed my initial answer but you didn't. All you had to do was create the new instance outside the Start() function.

2.You can move obstacleDictionary = new Dictionary<string, GameObject>(); from the Start() function to the Awake() function and it will still work.

Your new code:

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 the Execution Order of your script and make PlayerMove execute first before others. enter image description here

then

enter image description here

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • This is not work, there is my project, please check it https://1drv.ms/u/s!AsCxyl5DQ6LHnXch-oX8HW2OaIx9 –  Jun 18 '16 at 21:46
  • @Adam You tried my solution under 1 minute? Great! Put some effort to get further help. Try it! – Programmer Jun 18 '16 at 21:48
  • Because i tried it before you. All is in my project –  Jun 18 '16 at 21:51
  • @Adam Look at my Edit. You did not follow my initial answer. Provided 2 possible ways to fix this. – Programmer Jun 18 '16 at 22:37
  • First I want to apologize that I didn't answer. I slept on keyboard. :D Thank you so much for your patience with me, it works. Which way is best? Is there any difference? Thank you very much :) –  Jun 19 '16 at 04:43
  • @Adam The first or the second one. I suggest you go with the first one. Don't do the last one. It should only be used as a last resort Don't forget to tick this answer if your problem is solved. – Programmer Jun 19 '16 at 05:43