Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Referencing Dictionary from another script

Discussion in 'Scripting' started by Falihin, May 7, 2014.

  1. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    Hi,

    Is it possible to reference/access a dictionary created in a different script?
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    yes
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It's the same as accessing any other public member variable from another script. Why should it be different?

    Or more to the point, have you come across an issue that implies it is different, and can we help you fix THAT issue?
     
  4. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    I just started scripting with C#, so there are lots of things that I am not familiar with.
    Can anyone share an example on how to do this.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
  6. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Really basic example:
    Code (csharp):
    1. class A
    2. {
    3.    public Dictionary<string,GameObject> namedObjects;
    4. }
    5.  
    6. class B
    7. {
    8.    public void Foo (A a)
    9.    {
    10.       Debug.Log("There are " + a.namedObjects.Count + " named objects.");
    11.    }
    12. }
    Often you'd want this to be a property instead of a directly public variable, or have A just return specific items from it as needed, but that's the basic idea.