- Home /
Destroying multiple objects sometimes destroys more or less
I am working on a game on Unity, the concept is simple, you have to pick ingredients that fall from the top and stack them, when there are three ingredients of the same type, i destroy them, in the type of a Candy Crush style.
It works fine if i keep stacking elements of the same color, they keep getting destroyed as expected. The problem occurs when there are a lot of ingredients stacked up, where sometimes, 5 elements gets destroyed or 2 elements get destroyed instead of three.
So somewhere along the way of accumulating new objects and destroying those of the same color, sometimes 5 objects get destroyed, sometimes its 2.
It doesn't happen all the time, but it happens quite often, but it seems to be random, i couldn't isolate the case where it happens.
Here is the code i wrote in the Update method of the PlateManager.cs :
void Update()
{
if (countSameIngredients > 2)
{
//Destroying last 3 same ingredients
Destroy(ingredients[ingredients.Count - 3].gameObject);
Destroy(ingredients[ingredients.Count - 2].gameObject);
Destroy(ingredients[ingredients.Count - 1].gameObject);
ingredients.RemoveAt(ingredients.Count - 3);
ingredients.RemoveAt(ingredients.Count - 2);
ingredients.RemoveAt(ingredients.Count - 1);
//Checking if the plate is not empty
if (ingredients.Count > 0)
{
//If the plate is not empty, get the two last elements
int last = ingredients.Count - 1;
int beforeLast = ingredients.Count - 2;
//Save the last element type
lastIngredientType = ingredients[last].ingType;
//Compare the two last elements remaining, to see if they are similar
if (beforeLast >= 0 && last >= 0 && ingredients[beforeLast].ingType.Equals(lastIngredientType))
countSameIngredients = 2; //Two similar ingredients on the top of the plate
else
countSameIngredients = 1; //Only one element of that color on the top of the plate
}
else
{
//If the list is empty, set these options to their default parameters
lastIngredientType = "";
countSameIngredients = 0;
}
//Since we destroy the three last same ingredients, we move back the detection zone 3 times back
MoveBoxColliderCenterDown(3);
}
}
This is the code that destroys the element when there is finally 3 elements of the same type.
To explain the game further, i have a detection zone, which is triggered when an ingredient enters it. Whenever an ingredient enters the trigger zone, i add it to the list of ingredients and check if the count variable should be incremented or set back to one, here is the code that adds and checks those conditions ( inside the Ingredient.cs file ) :
PlateManager.ingredients.Add(gameObject.GetComponent<Ingredient>());
if (PlateManager.lastIngredientType == "")
{
PlateManager.countSameIngredients++;
PlateManager.lastIngredientType = gameObject.GetComponent<Ingredient>().ingType;
}
else if (PlateManager.lastIngredientType != "" && PlateManager.lastIngredientType.Equals(gameObject.GetComponent<Ingredient>().ingType))
{
PlateManager.countSameIngredients++;
PlateManager.lastIngredientType = gameObject.GetComponent<Ingredient>().ingType;
}
else if (PlateManager.lastIngredientType != "" && !PlateManager.lastIngredientType.Equals(gameObject.GetComponent<Ingredient>().ingType))
{
PlateManager.countSameIngredients = 1;
PlateManager.lastIngredientType = gameObject.GetComponent<Ingredient>().ingType;
}
(ingType variable means "Type of Ingredient" )
I don't understand why it doesn't always work as expected, i can't see it in the code and I need help.
Here is the link to the gitlab : https://gitlab.com/Shyrro/makeasandwich, where you can find the whole project, you can also just download the Builds folder if you want to test it by yourself. I am also open to any other help or advice. Thanks
[1]: /storage/temp/128304-screengame.png
Hey, I think your logic is solid, so I'm really intrigued with this one. I also have to say I found your code hard to read, to put it mildly :) I would check your project and all, but I just can't sign-up to this gitlab or anything for that matter just for the sake of checking/downloading a project when there are already open alternatives out there. But again, I'm intrigued and will be following this closely and honestly wish you good luck.
With a quick look it seems you mishandle the countSameIngredients var somewhere. That comes as a result, most probably, of manipulating the state of Platemanager externally, meaning the ingredient shouldn't change any values of Plate$$anonymous$$anager directly rather notify the Plate$$anonymous$$anager of its existance and let it handle it internally, well not even that, you could move the OnTriggerEnter inside the Plate$$anonymous$$anager, while leaving the ingredient in bliss.
Where you apply any changes directly in the same event and not in Update. As you know when something change you don't want to check every second.
@ecv80 @PizzaPie I just saw your comments, sorry i didn't answer earlier i didn't get notified for your comments. I actually moved the code inside the OnTriggerEnter, which also makes it easier to debug. I also moved the logic that was present in the Ingredient.cs to the Project$$anonymous$$anager.cs, the behaviour has changed, right now it always destroys 3, but sometimes misses the fact that there is one existing block to delete, so it deletes 3 blocks of the same color when i stack 4 for example. Again, this is something that doesn't happen all the time, i am trying to debug step by step but unity gets buggy when i do that, guess i'll have to debug with the Debug.Log method ins$$anonymous$$d of checking the code step by step
Answer by zsahmane · Nov 26, 2018 at 11:45 AM
I actually just found the answer. The detection zone was sometimes too close to the elements on the plate, which caused it to trigger multiple times at times, which messed with the counter.
I removed the code from the Update and put it in the OnTriggerEnter method, more efficient and I removed the collision between the ingredients on the plate and the detection zone and now it's working quite fine.
The gitlab is now private since i already found the answer, but i will let the post here as it may help someone in the future. If you're interested in the gitlab code though, you can contact me for access.
Your answer
Follow this Question
Related Questions
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers
How to destroy multiple box colliders through C# script 1 Answer
Execution order of Destroy() and DontDestroyOnLoad() between Scenes 0 Answers
My prefab isn't getting destroyed 1 Answer
how to destroy a separate object on touch with another object 6 Answers