• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by slampants · Nov 13, 2014 at 09:35 PM · arrayshashtable

Can an array be a "value" in a Key-Value pair/hashtable?

I've been tearing my hair out for a week now trying to build a dialogue system where all of the lines of a particular interaction are held within an array, and trying to make that array be a value in a Hashtable where the key is just a simple concatenation of two strings that can be grabbed from the scene. But I keep running into trouble when I build the hashtable, where I can assign the arrays as values, but when I use the key, it returns nothing. And workarounds either pass casting issues, slicing issues or "NullReferenceException: Object reference not set to an instance of an object." I know this is a complicated explanation, and the problem was getting overwhelming, so I built a really simple setup to try and sort out the problem. I have two scripts in this test.

_TestScriptA:

 var otherScript: _TestScriptB;
 var Box1 : GameObject;
 var Box2 : GameObject;
 var hash : Hashtable;
 
 function Start () {
     Box1 = GameObject.Find("GUI Text 1");
     Box2 = GameObject.Find("GUI Text 2");
     otherScript = GameObject.Find("Main Camera").GetComponent("_TestScriptB");
     hash = otherScript.hash;
 }
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.Q)){
         var x: Array = hash["poop"];
         Box1.guiText.text = x[0];
     }
 }

_TestScriptB:

 var poop: Array;
 var poop2: Array;
 var hash: Hashtable;
 
 function Start () {
     poop = new Array("1","2","3","4");
     poop2 = new Array("7","8","9","0");
     hash = new Hashtable();
     hash["poop"] = poop;
     hash["poop2"] = poop2;
 }

I know it's gotten a little messy, I've tried several iterations to try and resolve the problem. Tried to cut out anything that wasn't relevant. Hopefully what I'm attempting is clear here: When you push "Q," the Gui Text of Box1 should become the string "1."

Any help is appreciated! Using Unityscript, in case that's not clear.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Yword · Nov 14, 2014 at 07:49 AM

In the Start() function of _TestScriptA, it tries to access the "hash" of _TestScriptB, but the "hash" of _TestScriptB is initialized in Start() also.

So if the Start() function of _TestScriptA is executed first, then it will get null for the "hash" of _TestScriptB.

You can solve the problem by changing of Start() of _TestScriptB to Awake(), or you can change the execution order of the scripts at Edit->Project Settings->Script Execution Order.

Here is the modified _TestScriptA:

 #pragma strict
 
 var otherScript: _TestScriptB;
 var Box1 : GameObject;
 var Box2 : GameObject;
 var hash : Hashtable;
  
 function Start () {
     Box1 = GameObject.Find("GUI Text 1");
     Box2 = GameObject.Find("GUI Text 2");
     otherScript = GameObject.Find("Main Camera").GetComponent.<_TestScriptB>();
     hash = otherScript.hash;
 }
  
 function Update () {
     if(Input.GetKeyDown(KeyCode.Q)){
      var x: Array = hash["poop"] as Array;
      Box1.guiText.text = x[0] as String;
     }
 }

Here is the modified _TestScriptB:

 #pragma strict
 
 var poop: Array;
 var poop2: Array;
 var hash: Hashtable;
 
 function Awake () {
     poop = new Array("1","2","3","4");
     poop2 = new Array("7","8","9","0");
     hash = new Hashtable();
     hash["poop"] = poop;
     hash["poop2"] = poop2;
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image slampants · Nov 14, 2014 at 03:02 PM 0
Share

Thank you! Can't believe it was so simple. Gonna dig into my actual game script and see if I can make this solution apply to it. Will comment again if I have any trouble. Appreciate the help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Convert Object to a Float 1 Answer

creating and manipulating a grid efficiently 1 Answer

Dynamic multi-dimensional array in C#? 2 Answers

accessing gameObject script from an array 3 Answers

ios cant access variables in array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges