- Home /
Destroy Clone Game Object On Collision Doesn't work
Hey Guys,
I'm trying to make Block Breaker game. Now, I'm trying to create random power ups as a chance %5 when a block is destroyed.
This is where I instantiate a game object randomly.
public class BlockSquare : MonoBehaviour
{
[SerializeField] GameObject specPow;
public GameObject spcPow;
public void SpecPowInstantiate()
{
int randomNum = UnityEngine.Random.Range(0,20 );
if(randomNum == 10)
{
spcPow = Instantiate(specPow, transform.position, transform.rotation) as GameObject;
Destroy(spcPow, 5f);
}
}
}
This is where I tried to destroy clone gameobjects. But I couldnt :
public class Paddle : MonoBehaviour
{
BlockSquare blckSpecPow;
private void OnTriggerEnter2D(Collider2D collision)
{
if(gameObject.tag == "SpecPowGameObject")
{
Destroy(blckSpecPow.spcPow);
}
}
}
Answer by GGfazo · Nov 19, 2020 at 03:00 PM
I think the problem is in the second script, in the part where it says , because it should be like this cause this way it will search if the other objects tag is that and no the object tag of the script. If that doesn't work maybe the problem could be in the collider, rigidbidy or the Triggered option. But i think is the first thing.
Sorry but I didn't understand what you meant. Could you explain clearly pls ?
sorry the Important part of the message get erased for some reason I'll explain it. Where it says gameObject.tag == "SpecPowGameObject" it should be other.gameObject.tag == "SpecPowGameObject" because this way will comprove the other object tag and not hte tag of the gameobject that is "holding" the script.
Answer by alpayoguz97 · Nov 19, 2020 at 03:28 PM
I tried what you said. But that didn't change anything. Thanks for your answer. Any other thoughts ?
I suggest to change directly Destroy(blckSpecPow.spcPow) for Destroy(GameObject.FindWithTag("SpecPowGameObject")) and then comprove it works
Omg. Sorry but when I tried what you said above again, it fixed.
Thanks for your interest. I was trying to fix this since this morning.... (about 8 hours). Really thanks again !
Answer by logicandchaos · Nov 19, 2020 at 04:04 PM
Check that your tag is correct on your gameObject.
Your answer
Follow this Question
Related Questions
Ghost collisions problem: RigidBody player bounces, when moving on platform edges 2 Answers
PolygonCollider2d Use Z Axis 0 Answers
How to optimize a lot of colliders? 1 Answer
Box Colliders Merging 0 Answers
colliders arenot colliding 1 Answer