Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by RyanAchtenSoma · Nov 22, 2015 at 07:39 AM · floatvaluedictionarygeneric

Accessing float from dictionary

Hi Unity Community

I am using the generic dictionary to store a string [key] and a float [value], however I can’t seem to access it the float value by passing the string key.

I am sure the float values are present as they return properly when iterated over using the the key value pairs, so I must be trying to access the value wrong; can anyone enlighten me as to where this issue may be occurring?

Have tried a whole number of approaches available in reference documentation to no avail.

Many thanks in advance, Ryan

Dictionary declaration:

     groupingsOrigin = new Dictionary.<String, float>();
     
     for (var group : String in groupings)
     {    
         var subDivLength = floorSizeX - floorSubdivisionSize;
         floorSizeX -= floorSubdivisionSize; 
         var subDivXOrigin = subDivLength + (floorSubdivisionSize/2);
         groupingsOrigin.Add(group, subDivXOrigin);
     }

Debugs correct:

 for (var group : KeyValuePair.<String, float> in groupingsOrigin)
     {
         var groupOrigin = groupingsOrigin.Values;
         Debug.Log("Dictionary entry= " + " Group Name: " +group.Key + " " + " Group X Origin: " + group.Value);
     
     }

Value retrieval issue: ('curSortField' being synonymous w/ the key string)

 var curObjXPos : float = groupingsOrigin[curSortField]; //value of objSort dict matches the key of the groupingsOrigin dict
         
 //        var curObjXPos : float;
 //        groupingsOrigin.TryGetValue(curSortField, curObjXPos);


Issue returns:

 CurObj moving to: CompilerGenerated.__BrowseImpSortPositionObj_positionObjects$callable1$137_61__
 UnityEngine.Debug:Log(Object)
 BrowseImpSortPositionObj:positionObjects(Dictionary`2) (at Assets/Scripts/MetaPipe_SceneBrowseScripts/BrowseImpSortPositionObj.js:137)
 BrowseImpSortPositionObj:getPhotogramLocations(List`1) (at Assets/Scripts/MetaPipe_SceneBrowseScripts/BrowseImpSortPositionObj.js:83)
 BrowseImpSortPositionObj:sortMode(List`1) (at Assets/Scripts/MetaPipe_SceneBrowseScripts/BrowseImpSortPositionObj.js:39)
 $:MoveNext() (at Assets/Scripts/MetaPipe_SceneBrowseScripts/BrowseImportObj.js:132)
 

Question Update

Full script being used:

 #pragma strict
 
 //function used to sort imported objs by their sort field and position them in groups
 
 import System.Linq;
 import System.Xml;
 import System.Collections.Generic;
 
 var infoCont : ObjInfoControl;
 
 //xml declarations
 var xmlRoot : XmlNode;
 
 //gui dependancies
 var photogramLocationToggle : Toggle;
 var floorPlane : GameObject; //floorplane objects sit on
 
 var curPosObjects : List.<GameObject>;
 var groupingsOrigin = Dictionary.<String, float>();
 
 
 function sortMode(curBrowseObjects : List.<GameObject>) //determines what sort of sorting is requried
 {
     curPosObjects = curBrowseObjects;
 
     if (photogramLocationToggle.isOn)
     {
         getPhotogramLocations(curPosObjects);    
     }    
 }
 
 
 //Photogram etc might need their own script
 function getPhotogramLocations (curPosObjects : List.<GameObject>) //used to determine number of locations in browse results
 {
     xmlRoot = infoCont.control.root;
 
     var curObjName : String;
 
     var objectPhotogramLocations : List.<String>  = new List.<String>(); //will be used to count up number of locations in browse results
     
     var objectLocationsDict = new Dictionary.<GameObject, String>();
     
     Debug.Log("Positioning now");
     for (var object : Object in curPosObjects)
     {
     
         var curObject = object as GameObject;
         curObjName = curObject.name;
         Debug.Log("Positioning: " + curObjName);
         
         var curObjNode = xmlRoot.SelectSingleNode("MetaPipeObject[@name='"+ curObjName +"']");
         var curLocationName = curObjNode.SelectSingleNode("./ModelInfo/PhotogramInfo/PhotogramLocation/PhotogramLocationName").InnerText;
         Debug.Log(curObjName + " location: " + curLocationName);    
         
         objectLocationsDict.Add(curObject, curLocationName);
         
         if (!objectPhotogramLocations.Contains(curLocationName)) //will only add location name to list if it doesn't already exist
         {
             objectPhotogramLocations.Add(curLocationName);
         }
     }
     
     //sort the photogram groups reverse alphabetically to match origin calculations
     objectPhotogramLocations = objectPhotogramLocations.OrderByDescending(function(curObjName) curObjName).ToList();
     
     var photogramLocationCount : int = objectPhotogramLocations.Count;
     Debug.Log("Total number of locations: " + photogramLocationCount);
     
     findXOrigin(photogramLocationCount, objectPhotogramLocations); //find floor allocations for the individual groupings
     
     positionObjects(objectLocationsDict); //move the objects into position
 }
 
 
 function findXOrigin(numberOfGroupings : int, groupings : List.<String>)
 {
     var floorPlaneCol = floorPlane.GetComponent(BoxCollider);    
     var floorSizeX = floorPlaneCol.size.x;
     Debug.Log("floorSizeX: " + floorSizeX);
     
     var floorSubdivisionSize = floorSizeX / numberOfGroupings;
     Debug.Log("floorSubdivisionSize: " + floorSubdivisionSize);
     
     groupingsOrigin = new Dictionary.<String, float>();
     
     for (var group : String in groupings)
     {    
         var subDivLength = floorSizeX - floorSubdivisionSize;
         floorSizeX -= floorSubdivisionSize; //removes accounted for section from the overall size
         var subDivXOrigin = subDivLength + (floorSubdivisionSize/2);
         groupingsOrigin.Add(group, subDivXOrigin);
     }
     
     //Accessing values example
     for (var group : KeyValuePair.<String, float> in groupingsOrigin)
     {
         var groupOrigin = groupingsOrigin.Values;
         Debug.Log("Dictionary entry= " + " Group Name: " +group.Key + " " + " Group X Origin: " + group.Value);
     
     }
     
     for (var group : String in groupingsOrigin.Keys)
     {
         var groupOriginX = groupingsOrigin[group];
         Debug.Log("Dictionary entry= " + " Group Name: " + group + " " + " Group X Origin: " + groupOriginX.ToString);
     }
 }
 
 
 function positionObjects(objectSortDict : Dictionary.<GameObject, String>)
 {
     //assigns rb/coll components and moves objects into position
     
     //var groupingsOrigin = Dictionary.<String, float>()
     
     for (var browseObj : KeyValuePair.<GameObject, String> in objectSortDict)
     {
         var curObj : GameObject = browseObj.Key;
         var curSortField : String = browseObj.Value;
         Debug.Log("Dictionary entry= " + " Object Name: " + curObj.name + " " + " Location: " + curSortField);
         
         var curObjXPos : float = groupingsOrigin[curSortField]; //value of objSort dict matches the key of the groupingsOrigin dict
         
 //        var curObjXPos : float;
 //        groupingsOrigin.TryGetValue(curSortField, curObjXPos);
         
         curObj.transform.position.x = curObjXPos;
         Debug.Log("CurObj moving to: " + curObjXPos.ToString);
     }
 
 }


Comment
Add comment · Show 3
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 tanoshimi · Nov 22, 2015 at 07:50 AM 0
Share

I don't use Unityscript, but can't see anything obviously wrong with your logic.

The error appears to be complaining about "CurObj" - where/how is that defined? Likewise, how is the accessor curSortField defined?

avatar image RyanAchtenSoma tanoshimi · Nov 22, 2015 at 08:17 AM 0
Share

Hi @tanoshimi, thanks for your quick response.

The 'Value retrieval issue' is occurring inside of the following function:

 function positionObjects(objectSortDict : Dictionary.<GameObject, String>)
 {
     //assigns rb/coll components and moves objects into position
     
     //var groupingsOrigin = Dictionary.<String, float>()
     
     for (var object : $$anonymous$$eyValuePair.<GameObject, String> in objectSortDict)
     {
         var curObj : GameObject = object.$$anonymous$$ey;
         var curSortField : String = object.Value;
         Debug.Log("Dictionary entry= " + " Object Name: " + curObj.name + " " + " Location: " + curSortField);
         
         var curObjXPos : float = groupingsOrigin[curSortField]; //value of objSort dict matches the key of the groupingsOrigin dict
         
 //        var curObjXPos : float;
 //        groupingsOrigin.TryGetValue(curSortField, curObjXPos);
         
         curObj.transform.position.x = curObjXPos;
         Debug.Log("CurObj moving to: " + curObjXPos.ToString);
     }
 
 }

avatar image RyanAchtenSoma · Nov 24, 2015 at 09:21 AM 0
Share

Note: the recommended .TryGetValue method also does not work; suggestions?

2 Replies

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

Answer by RyanAchtenSoma · Nov 24, 2015 at 09:31 PM

Turns out the issue was being caused by an incorrect Debug.Log where .ToString was trying to be parsed in debugging sequences such as in:

 Debug.Log("Dictionary entry= " + " Group Name: " + group + " " + " Group X Origin: " + groupOriginX.ToString);

Not the actual dictionary references themselves.

Cheers, Ryan

Comment
Add comment · 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
0

Answer by tanoshimi · Nov 22, 2015 at 09:24 AM

Just a guess, but object is a reserved keyword in most languages. You should try replacing your variable name with something else:

 for (var myObject : KeyValuePair.<GameObject, String> in objectSortDict) {

     var curObj : GameObject = myObject.Key;
     var curSortField : String = myObject.Value;
   ....
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 RyanAchtenSoma · Nov 22, 2015 at 09:43 PM 0
Share

Hi @tanoshimi, that's good to know; have changed the variable name but the problem seems to still occur.

It also arises when trying to debug.log the dictionary contents using the key index as opposed to the key value pairs.

i.e.

Works:

     for (var group : $$anonymous$$eyValuePair.<String, float> in groupingsOrigin)
     {
         var groupOrigin = groupingsOrigin.Values;
         Debug.Log("Dictionary entry= " + " Group Name: " +group.$$anonymous$$ey + " " + " Group X Origin: " + group.Value);
     
     }

Does not work:

 for (var group : String in groupingsOrigin.$$anonymous$$eys)
     {
         var groupOriginX = groupingsOrigin[group];
         Debug.Log("Dictionary entry= " + " Group Name: " + group + " " + " Group X Origin: " + groupOriginX.ToString);
     }

Will update my question to include the whole script being used. Thanks for all your help, Ryan.

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.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dictionary doesn't exist? 1 Answer

zero is not zero 2 Answers

Can I get a reference (not a copy) to a string from a script? 2 Answers

Reference to value type 1 Answer

JS: Dictionary is not a generic definition error... 3 Answers


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