- Home /
Why does it seem as if my code is exuting in wrong order?
So I have a problem where I try to search for a pooled object in categorized Object Pools. I.E - I have one Object Pool for Metal Pickaxes, another pool for Metal Hatchets. When I craft new objects, I create new inactivated gameobjects to fill the pools.
I have correctly written the order of my code, but it is as if Instantiate seems to be too slow so when I try to call a method to search for matches in the object pool, it seems as if my item pool doesn't search for any new currently available items in the pool. So when I instantiate and add my item to the pool, it lands into the pool after the comparison checks with my for-loops. Even though I specifically type my code to pool my object in first-hand and then search for matches after that.
Code used below:
The code down here is the main code where I instantiate and pool a newly crafted item (gives null as we cant find the correct comparison child in the code coming after this one due to Transform.child out of range error and no new item being added to our pool.)
public void InstantiateCraftedItem(ItemDatabase item)
{
Camera weaponCamera = GameObject.Find("Camera").GetComponent<Camera>();
invTracker = GameObject.Find("InventoryTracker");
GameObject playerHand = GameObject.Find("HoldableObjectSpawnPoint");
var instantiatedEquippedItem = Instantiate(item.equipPrefab, equipPosition.position, // Instantiate the item to hold
weaponCamera.transform.rotation/*, item.equipPrefab.transform.parent = equipPosition.transform*/); // Rotate it to match the cameras rotation
// Generating GUID
IGenerateGUID currentInstObjGUID = instantiatedEquippedItem.GetComponent<IGenerateGUID>();
currentInstObjGUID.GenerateGuid();
string instObjGUID = currentInstObjGUID.GetGUID();
string fullName = item.displayName + " #" + instObjGUID;
instantiatedEquippedItem.name = fullName;
//currentInstObjGUID.WasCrafted(true);
//instantiatedEquippedItem.GetComponent<ItemObject>().PoolThisObject();
//GameObject foundGameObject = CustomPooler.Instance.ReturnFoundGameObjectInPool(fullName);
Debug.Log("Fullname is: " + instantiatedEquippedItem.transform.gameObject.name);
//Debug.Log("2Fullname is: =================================================================================================================================");
GameObject foundGameObject = CustomPooler.Instance.ReturnFoundGameObjectInPool(instantiatedEquippedItem.name);
//foundGameObject.transform.parent = playerHand.transform;
//foundGameObject.transform.position = playerHand.transform.position;
//foundGameObject.transform.gameObject.SetActive(false);
//Destroy(instantiatedEquippedItem);
//Destroy(foundGameObject);
Debug.Log("Fullname is: =================================================================================================================================");
}
Next code piece of code that handles my lookup in my transforms and their children:
public void PoolNextObject(GameObject go)
{
// We first need to check if that pool already exists. If it does, we don't need to create a new pool, only add to existing pool
for (int k = 0; k <= transform.childCount; k++)
{
// Look for a child on the transform of the attached script which has a pool that contains the name
// of the item we would like to dynamically instantiate into the list.
string compareName = go.transform.GetComponent<ItemObject>().poolObjectName; // We only look for item name w/o ID.
// Start to look if we have any children at all. If not, create a pool child.
if (transform.childCount == 0)
{
Pool pool = new Pool();
Queue<GameObject> objectPool = new Queue<GameObject>();
if (!pool.hasParent)
{
//If this object was just created, this should be false so we can create a parent for it.
pool.poolHolder = new GameObject(go.GetComponent<ItemObject>().poolObjectName + " Pool");
pool.poolHolder.transform.parent = Instance.transform;
//We'll set hasParent to true here so that we won't have many parents for this object
pool.hasParent = true;
}
poolDictionary = new Dictionary<string, Queue<GameObject>>();
GameObject newObject = Instantiate(go);
newObject.SetActive(false);
newObject.name = go.GetComponent<ItemObject>().name; // Rename so we don't see "(Clone)".
newObject.transform.parent = pool.poolHolder.transform; //Set the parent here once we create the game object
objectPool.Enqueue(newObject); //Then add it to our list.
pool.tag = UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString() + UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString();
//poolDictionary.Add(pool.tag, objectPool);
continue; // Skip iteration to avoid bugs.
}
else if (transform.GetChild(k).gameObject.name.Contains(compareName) == true)// We found our pool for our Game Object's name.
{
// Child is now pool object (current pool).
currentPool = transform.GetChild(k).gameObject;
Pool pool = new Pool();
Queue<GameObject> objectPool = new Queue<GameObject>();
GameObject newObject = Instantiate(go);
newObject.SetActive(false);
newObject.name = go.GetComponent<ItemObject>().name; // Rename so we don't see "(Clone)".
newObject.transform.parent = currentPool.transform; //Set the parent here once we create the game object
objectPool.Enqueue(newObject); //Then add it to our list!
pool.tag = UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString() + UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString();
poolDictionary.Add(pool.tag, objectPool);
break;
}
else if (!transform.GetChild(k).gameObject.name.Contains(compareName) && k == transform.childCount-1)// Create a new object pool if none are available
{
Pool pool = new Pool();
Queue<GameObject> objectPool = new Queue<GameObject>();
if (!pool.hasParent)
{
pool.poolHolder = new GameObject(go.GetComponent<ItemObject>().poolObjectName + " Pool");
pool.poolHolder.transform.parent = Instance.transform;
pool.hasParent = true;
}
poolDictionary = new Dictionary<string, Queue<GameObject>>();
// We could create a for-loop here if we wish to set a parameter
// for the game to decide how many new items to spawn. Not relevant now though.
GameObject newObject = Instantiate(go);
newObject.SetActive(false);
newObject.name = go.GetComponent<ItemObject>().name;
newObject.transform.parent = pool.poolHolder.transform;
objectPool.Enqueue(newObject);
pool.tag = UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString() + UnityEngine.Random.Range(-10000.0f, 10000.0f).ToString();
poolDictionary.Add(pool.tag, objectPool);
continue;
}
// If we don't find a child at all matching the name, skip this iteration.
else if (!transform.GetChild(k).gameObject.name.Contains(compareName) == true)
continue;
}
}
if more code is needed, I'll include it if you ask for it, but I think this should suffice.
Hard to follow your code but I'm pretty sure you are making this too complicated. I suggest to break the code to smaller methods with simple functionality so it's easier to debug.
Anyway when you are pooling objects I suggest to use statically accessible List (or Dictionary) to store the pooled items. You can also make singleton with static reference that will have all the pools you need. Here is pretty basic Unity instructions for object pooling: https://learn.unity.com/tutorial/introduction-to-object-pooling#
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
Rpreoduce csv or delay iteration 0 Answers
Can big problems occur when changing execution order of scripts? 1 Answer