- Home /
Objects stop being Destroyed. No idea why!
This is a weird issue, and the question is the closest I can get to explaining it
Basically my project just seems to work for 3 layers of objects and when it reaches the 4th layer it won't do anything. This is what I mean: https://youtu.be/BbaXvePqYDc In the video you will see that I dig down 3 layers and can remove the objects "dirt" on it's side but when I go to the fourth layer it does nothing.
I've narrowed the issue to 1 line of code but that same line works for everything before it.
This is the relevant code:
Determines at what y coord the player is
void Update () {
if (this.transform.position.y == -10.54467f) {
minerLevel = "DirtLvl1";
}
else if(this.transform.position.y == -12.46467f) {
minerLevel = "DirtLvl2";
}
else if(this.transform.position.y == -14.38467f) {
minerLevel = "DirtLvl3";
}
else if(this.transform.position.y == -16.30467f) {
minerLevel = "DirtLvl4";
}
else if(this.transform.position.y == -18.22467f) {
minerLevel = "DirtLvl5";
}
else if(this.transform.position.y == -20.14467f) {
minerLevel = "DirtLvl6";
}
else if(this.transform.position.y == -22.06467f) {
minerLevel = "DirtLvl7";
}
else if(this.transform.position.y == -23.98467f) {
minerLevel = "DirtLvl8";
}
else if(this.transform.position.y == -25.90467f) {
minerLevel = "DirtLvl9";
}
else if(this.transform.position.y == -27.82467f) {
minerLevel = "DirtLvl10";
}
}
This determines if the player is within the correct parameters and if so allowes the object to be destroyed using S (down) and A (left).
void OnCollisionStay2D(Collision2D collStay)
{
if (Input.GetKey ("s") && triggered == false) {
countDown -= Time.deltaTime;
if (countDown <= 0) {
for (int i = 0; i < 8; i++) {
if (collStay.transform.tag == "DirtLvl" + i) {
Destroy (collStay.gameObject);
countDown = 2.0f;
}
}
}
}
if (entered == true) {
if (Input.GetKey ("a")) {
countDown -= Time.deltaTime;
if (countDown <= 0) {
for (int i = 0; i < 8; i++) {
if (minerLevel == "DirtLvl" + i) {
if (collStay.transform.tag == "DirtLvl" + i) {
Destroy (collStay.gameObject);
countDown = 2.0f;
transform.Translate (-0.15f, 0.0f, 0.0f);
}
}
}
}
}
}
}
I've found the issue comes in with if (minerLevel == "DirtLvl" + i)
but I don't see how that makes sense because it working for the first 3 elevations but nothing further than that.
If there's anymore info needed please let me know. I've already completely rewritten the before because of a similar issue and I don't want to do it again.
Thanks for any help
Answer by TreyH · May 09, 2018 at 11:55 AM
Do not use exact comparisons on floating-point values.
You're doing two weird things here, so we'll address them both. First, you shouldn't compare floating point values using equality. Since you're doing things apparently in terms of depth, you can probably replace those with a " than" comparison:
if (this.transform.position.y >= -10.54467f && this.transform.position.y <= -10) {
minerLevel = "DirtLvl1";
}
else if(this.transform.position.y >= -12.46467f) {
minerLevel = "DirtLvl2"
}
// ...
Secondly (and your actual compilation issue), you can't add strings and integers like that. You'll need to convert the index to a string and create a new string with that index appended onto your other one:
for (int i = 0; i < 8; i++) {
// Need to append the level index to the string
string iterLevel = "DirtLvl" + i.ToString();
if (minerLevel == iterLevel) {
if (collStay.transform.tag == iterLevel) {
Destroy (collStay.gameObject);
countDown = 2.0f;
transform.Translate (-0.15f, 0.0f, 0.0f);
}
}
}
Is there a reason why it works for the first 3 layers but not after that?
How are your levels laid out? Those are some see$$anonymous$$gly random values, is there any reason you chose them?
If you look at the video you see me dig through the layers of dirt, those are the levels. The $$anonymous$$erLevels that you see in the code is based on the y coord of the player when it's on top of the next layer. I used print (this.transform.postion.y);
to get the y coord for each level.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162316im_/https://answers.unity.com/themes/thub/images/avi.jpg)