Destroying multiple objects sometimes destroys more or less than expected
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.RemoveRange(ingredients.Count - 3, 3);
//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
c# unity3d
add a comment |
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.RemoveRange(ingredients.Count - 3, 3);
//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
c# unity3d
Try replacing yourRemoveAt()
s with one line:ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case theCount
property isn't updating as expected.
– 0liveradam8
Nov 25 '18 at 2:22
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?
– Draco18s
Nov 25 '18 at 2:31
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35
add a comment |
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.RemoveRange(ingredients.Count - 3, 3);
//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
c# unity3d
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.RemoveRange(ingredients.Count - 3, 3);
//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
c# unity3d
c# unity3d
edited Nov 26 '18 at 11:38
Zakaria Sahmane
asked Nov 25 '18 at 2:04
Zakaria SahmaneZakaria Sahmane
11810
11810
Try replacing yourRemoveAt()
s with one line:ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case theCount
property isn't updating as expected.
– 0liveradam8
Nov 25 '18 at 2:22
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?
– Draco18s
Nov 25 '18 at 2:31
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35
add a comment |
Try replacing yourRemoveAt()
s with one line:ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case theCount
property isn't updating as expected.
– 0liveradam8
Nov 25 '18 at 2:22
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?
– Draco18s
Nov 25 '18 at 2:31
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35
Try replacing your
RemoveAt()
s with one line: ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case the Count
property isn't updating as expected.– 0liveradam8
Nov 25 '18 at 2:22
Try replacing your
RemoveAt()
s with one line: ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case the Count
property isn't updating as expected.– 0liveradam8
Nov 25 '18 at 2:22
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?– Draco18s
Nov 25 '18 at 2:31
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?– Draco18s
Nov 25 '18 at 2:31
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35
add a comment |
2 Answers
2
active
oldest
votes
I have checked the project in the link you provided. I would change the following:
You are using a:
void OnCollisionEnter(Collision other)
{
if (other.collider.tag.Equals("Ground"))
{
Physics.IgnoreCollision(other.collider, boxCollider, true);
}
}
I would add instead a onTriggerEnter()
check it here
Then, you need to take into account that the Update() is called several times per second, but main action you are performing in the Update() is to check whether you need to destroy some ingredients, which actually should be checked once, when a new ingredient arrives to the stack. I believe that may be the problem.
Try to move the logic inside the Update() to an external function which will be called when the OnTriggerEnter() is triggered (that is when you have a new ingredient in the stack).
Something like:
private void OnTriggerEnter(Collider other)
{
checkPlate();
}
void checkPlate(){
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);
...
}
...
}
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
add a comment |
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 just removed the collision between the ingredients on the plate and the detection 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464066%2fdestroying-multiple-objects-sometimes-destroys-more-or-less-than-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have checked the project in the link you provided. I would change the following:
You are using a:
void OnCollisionEnter(Collision other)
{
if (other.collider.tag.Equals("Ground"))
{
Physics.IgnoreCollision(other.collider, boxCollider, true);
}
}
I would add instead a onTriggerEnter()
check it here
Then, you need to take into account that the Update() is called several times per second, but main action you are performing in the Update() is to check whether you need to destroy some ingredients, which actually should be checked once, when a new ingredient arrives to the stack. I believe that may be the problem.
Try to move the logic inside the Update() to an external function which will be called when the OnTriggerEnter() is triggered (that is when you have a new ingredient in the stack).
Something like:
private void OnTriggerEnter(Collider other)
{
checkPlate();
}
void checkPlate(){
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);
...
}
...
}
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
add a comment |
I have checked the project in the link you provided. I would change the following:
You are using a:
void OnCollisionEnter(Collision other)
{
if (other.collider.tag.Equals("Ground"))
{
Physics.IgnoreCollision(other.collider, boxCollider, true);
}
}
I would add instead a onTriggerEnter()
check it here
Then, you need to take into account that the Update() is called several times per second, but main action you are performing in the Update() is to check whether you need to destroy some ingredients, which actually should be checked once, when a new ingredient arrives to the stack. I believe that may be the problem.
Try to move the logic inside the Update() to an external function which will be called when the OnTriggerEnter() is triggered (that is when you have a new ingredient in the stack).
Something like:
private void OnTriggerEnter(Collider other)
{
checkPlate();
}
void checkPlate(){
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);
...
}
...
}
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
add a comment |
I have checked the project in the link you provided. I would change the following:
You are using a:
void OnCollisionEnter(Collision other)
{
if (other.collider.tag.Equals("Ground"))
{
Physics.IgnoreCollision(other.collider, boxCollider, true);
}
}
I would add instead a onTriggerEnter()
check it here
Then, you need to take into account that the Update() is called several times per second, but main action you are performing in the Update() is to check whether you need to destroy some ingredients, which actually should be checked once, when a new ingredient arrives to the stack. I believe that may be the problem.
Try to move the logic inside the Update() to an external function which will be called when the OnTriggerEnter() is triggered (that is when you have a new ingredient in the stack).
Something like:
private void OnTriggerEnter(Collider other)
{
checkPlate();
}
void checkPlate(){
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);
...
}
...
}
I have checked the project in the link you provided. I would change the following:
You are using a:
void OnCollisionEnter(Collision other)
{
if (other.collider.tag.Equals("Ground"))
{
Physics.IgnoreCollision(other.collider, boxCollider, true);
}
}
I would add instead a onTriggerEnter()
check it here
Then, you need to take into account that the Update() is called several times per second, but main action you are performing in the Update() is to check whether you need to destroy some ingredients, which actually should be checked once, when a new ingredient arrives to the stack. I believe that may be the problem.
Try to move the logic inside the Update() to an external function which will be called when the OnTriggerEnter() is triggered (that is when you have a new ingredient in the stack).
Something like:
private void OnTriggerEnter(Collider other)
{
checkPlate();
}
void checkPlate(){
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);
...
}
...
}
answered Nov 25 '18 at 7:08
Ignacio AlorreIgnacio Alorre
3,07042749
3,07042749
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
add a comment |
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
I just tried that but the result seems to be the same. Thanks for the trigger advice though, easier to debug actually, i might find my answer since it's easier to debug on trigger than on each frame
– Zakaria Sahmane
Nov 25 '18 at 20:44
add a comment |
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 just removed the collision between the ingredients on the plate and the detection 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.
add a comment |
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 just removed the collision between the ingredients on the plate and the detection 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.
add a comment |
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 just removed the collision between the ingredients on the plate and the detection 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.
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 just removed the collision between the ingredients on the plate and the detection 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.
answered Nov 26 '18 at 11:38
Zakaria SahmaneZakaria Sahmane
11810
11810
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464066%2fdestroying-multiple-objects-sometimes-destroys-more-or-less-than-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try replacing your
RemoveAt()
s with one line:ingredients.RemoveRange(ingredients.Count - 3,3);
, just in case theCount
property isn't updating as expected.– 0liveradam8
Nov 25 '18 at 2:22
@0liveradam8 I just did as you said but the result was pretty much the same. I am going to let your line in the code because it is more readable though
– Zakaria Sahmane
Nov 25 '18 at 2:28
if (countSameIngredients > 2) { Destroy(3); }
What if you have a stack of 2 red, then 3 green, then 2 more red? Surely you'd want to destroy all four red after the greens are removed, no?– Draco18s
Nov 25 '18 at 2:31
@Draco18s That wouldn't(shouldn't?) happen since the 3 green boxes would be destroyed as soon as they are stacked on each other. Then the next red block, should form a block of 3 same red blocks which should also get destroyed right away. That's why i check how much blocks of the same color are on the top, to set the counter to either 1 or 2
– Zakaria Sahmane
Nov 25 '18 at 2:35