- Home /
Run through hastable of objects
Well I'm running through a hastable of objects in order to set them as active (they are unactive as default).
for (var item : DictionaryEntry in player1hand){ var temp : Transform = player1hand[item.Key]; temp.active =true; }
being player1hand the hastable. I have two keys but only activates one object, why Is that?
I have no idea what's your problem!?! So to sum up:
You have two items in the hashtable
Each with a unique key
You want to iterate through all items in the table
and activate the corresponding object
The way you do it, it should work. The only problem i can see is that you used the same key so there's only one item in the hashtable
there are two items in the hashtable. and the key is being iterated right? edit: and the items have diferent keys because I've tried manually already.
Are you sure that there are two items in the table? DictionaryEntry has both, the $$anonymous$$ey and the Value of each item.
Have you tried adding a Debug.Log inside the loop and let it print the key name. You should get two logs in the console if there are two items in the table.
edit
Example:
for (var item : DictionaryEntry in player1hand)
{
var temp : Transform = item.Value;
if (temp != null)
{
Debug.Log("$$anonymous$$ey: " + item.$$anonymous$$ey + " references a gameobject with the name: " + temp.gameObject.name);
}
else
{
Debug.Log("$$anonymous$$ey: " + item.$$anonymous$$ey + " is null!!!!!");
}
}
hmm. the key isnt the same, but for some reason the item is. the problem was in the filling :x for some reason this wasnt working:
var aux2 = Random.Range( 0, player1deck.Count ); player1hand.Add(player1hand.Count, player1deck[aux2]); aux2 = Random.Range( 0, player1deck.Count ); player1hand.Add(player1hand.Count, player1deck[aux2]);
I was trying to get a random card but isnt working. the aux2 always saves the same
Uhm, may i ask why you use a Hashtable? It seems you try to use it like an array or a List...
What is "player1deck"? is it a List or also a Hashtable? Where is player1deck filled and are you sure it's filled correctly?
Answer by Bunny83 · Dec 31, 2013 at 12:39 AM
I have a feeling that you actually don't need a Hashtable / Dictionary. An ordinary List will do.
You need to import the System.Collections.Generic
namespace at the top of your script in order to use the generic List.
// UnityScript
var player1deck = new List.<Transform>();
A List allows adding and removing of elements but behaves like an ordinary array.
player1deck.Add(T1);
player1deck.Add(T2);
player1deck.Add(T3);
print(player1deck.Count); // prints "3"
print(player1deck[0].name); // T1
print(player1deck[1].name); // T2
print(player1deck[2].name); // T3
player1deck.Remove(T2);
print(player1deck[0].name); // T1
print(player1deck[1].name); // T3
print(player1deck[2].name); // ERROR since there are only 2 elements in the list
player1deck.RemoveAt(0); // removes the first(index 0) element
print(player1deck.Count); // prints "1"
Your answer
Follow this Question
Related Questions
Move object to raycast point. 3 Answers
How To Deactivate A Parent (And Its Children)? 4 Answers
How to deactivate gameObjects instead of destroying them ? 1 Answer
Collision at which side? 1 Answer
Random instantiation at an object 1 Answer