Hi guys,
Im trying to destroy some objects from a Hastable, (basically destroy the objects to certain distance), the problem that i have is that I cant access to the older objects. I can only destroy the new ones.
I want to the destroy and generate objects at a runtime. So I think the problem im having is storing those objects.
//Resume of the code
private Hashtable objects = new Hastable();
void GenerateObject()
{
//Here I create diferent objects to different heights
for(blablabla)
{
GameObject target = GameObject.Instantiate(treeOnGrass, positionTreeGrass, Quaternion.identity) as GameObject;
string targetName = "TreeOnGrass_" + ((int)(positionTreeGrass.x)).ToString() + "_" + ((int)(positionTreeGrass.z)).ToString();
target.name = targetName;
TreesAndRocks treeAndRocks = new TreesAndRocks(target, positionTreeGrass);
if (!objects.ContainsKey(targetName))
{
objects.Add(targetName, treeAndRocks);
}
else {
(objects[targetName] as TreesAndRocks).positionObjects = positionTreeGrass;
}
}
//other object
for(bla bla bla)
GameObject target = GameObject.Instantiate(otherObject, positionTreeGrass, Quaternion.identity) as GameObject;
string targetName = "OtherObject" + ((int)(positionTreeGrass.x)).ToString() + "_" + ((int)(positionTreeGrass.z)).ToString();
target.name = targetName;
TreesAndRocks treeAndRocks = new TreesAndRocks(target, positionTreeGrass);
if (!objects.ContainsKey(targetName))
{
objects.Add(targetName, treeAndRocks);
}
else {
(objects[targetName] as TreesAndRocks).positionObjects = positionTreeGrass;
}
}
//Then I have a Destroy function But when I want to access to that Hastable I cant do it to the older objects, for example I cant destroy all
// the objects. Only the ones that are created each time that I generate in that moment
public void DestroyObjects()
{
//Destroy Far Trees and
Hashtable newObjects = new Hashtable();
foreach (TreesAndRocks tAr in objects.Values)
{
if (tAr.positionObjects.x > TerrainChunkGenerator.playerPosition.position.x && TerrainChunkGenerator.activateRemover)
{
UnityEngine.Object.Destroy(tAr.tressAndRocks);
Debug.Log(TerrainChunkGenerator.playerPosition.position.x + "_" + TerrainChunkGenerator.playerPosition.position.y + "_" + TerrainChunkGenerator.playerPosition.position.z);
}
else
{
newObjects.Add(tAr.tressAndRocks.name, tAr);
}
}
objects = newObjects;
}
So my queston is … How to ADD to a List, Hastable, o dictionarry, any object. And have the posibility to access later.???.. Thank you