- Home /
Combine Children Dictionary in place of Hashtable?
Hi,
In Combine Children script how to convert the hashtable to Dictionary.
and ArrayList to List? ? ?
Here is coding part of Combine children. .
void Awake()
{
Hashtable materialToMesh = new Hashtable();
ArrayList objects = (ArrayList)materialToMesh[materials[m]];
if (objects != null)
{ objects.Add(instance);
}
else
{
objects.Add(instance);
materialToMesh.Add(materials[m], objects);objects = new ArrayList();
}
If i changed the Hashtable and ArrayList like this Dictionary materialToMesh= new Dictionary(); List objects = (List)materialToMesh[materials[m]];
It shows and Error. .
Help me to solve this? ?
Answer by iwaldrop · Jan 16, 2013 at 11:16 PM
It's because you have to tell the compiler what types of data the Dictionary is going to hold:
Dictionary<byte, object> op = new Dictionary<byte, object>();
Additionally, in order to create Lists you need to do the following:
List<float> floats = new List<float>();
Yes. I did that, but still facing the error .
Dictionary materialTo$$anonymous$$esh= new Dictionary(); $$anonymous$$eshCombineUtility.$$anonymous$$eshInstance instance = new $$anonymous$$eshCombineUtility.$$anonymous$$eshInstance (); List objects = (List)materialTo$$anonymous$$esh[materials[m]];
Error it shows is: Error 114 Cannot convert type 'UnityEngine.$$anonymous$$esh' to 'System.Collections.Generic.List'
Well, the error says it all; you're attempting to cast the Value of materials[m] as a List (which appears to be a $$anonymous$$esh by looking the error).
If you're trying to take all the Values of a Dictionary and put them into a List then you need to do something like the following:
List<$$anonymous$$esh> meshes = new List<$$anonymous$$esh>();
materialTo$$anonymous$$esh.Values.ForEach(m => meshes.Add(m));
Here is the Link of the Script that i got error in the conversion. Look out it.
Neat script. Sorry, but this is a little above my pay grade. I hope that someone else can give you an answer. I've bumped it so maybe somebody will take notice. Good luck!
Answer by SARWAN · Jan 17, 2013 at 06:42 AM
@iwaldrop
Yes I Already did the same thing only. But i didn't put in the code part so only it shows like that. I changed that to
Dictionary<Material,Mesh> materialToMesh= new Dictionary<Material,Mesh>();
for (int i=0;i<filters.Length;i++) {
..
..
MeshCombineUtility.MeshInstance instance = new MeshCombineUtility.MeshInstance ();
...
...
...
Material[] materials = curRenderer.sharedMaterials;
for (int m=0;m<materials.Length;m++) {
instance.subMeshIndex = System.Math.Min(m, instance.mesh.subMeshCount - 1);
List<MeshCombineUtility.MeshInstance> objects = (List<MeshCombineUtility.MeshInstance>)materialToMesh[materials[m]];
But it shows an error as Error 114 Cannot convert type 'UnityEngine.Mesh' to 'System.Collections.Generic.List'
Your answer
Follow this Question
Related Questions
Serializing data structures the Unity way. Possible? 1 Answer
Hashset and ArrayList Not Updating Upon Button Click 2 Answers
Frame Drop on a function dealing with an updated List type (Only once after new element Added) 0 Answers
Is it possible to sort an array determined by an external comparison? 1 Answer
Multiple Cars not working 1 Answer