- Home /
How to destroy a gameObject only when inside a rigidBody
Hello ,
I'm trying to make a rythm music game, where notes in shape of sphere will drop down on a table and once user hit the correct key(1 to 6) , I want the correct note to explode depends on which rail is the note , So my logic goes like this :
As you can see I have an invisible rigid body , so while the note is inside of it , if user hit the correct key , I wanted the note to explode
and my note Control script goes like this :
void Update () {
if (Input.GetKeyDown(activate))
{
if (score)
{
Debug.Log(gameObject.name);
Destroy(gameObject);
Instantiate(succBurst, transform.position, succBurst.rotation);
score = false;
}
}
}
void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
if (other.gameObject.name == "FailedCol")
{
Destroy(gameObject);
Instantiate(failedBurst, transform.position, failedBurst.rotation);
}
if( (other.gameObject.name == "score1"))
{
//The Ballon is inside the scoring rigid body which is the invisible part on picture
score = true;
}
}
}
But it seems not to work properly , how to destroy the gameObject ( the note ) prefab Only when inside the rigid body? What is wrong with my code and what I have missed?
Thank you
To be honest, your particular case is probably better off in the forums. I can already see a few other problems with your current approach, that would exceed the scope of the answer system.
For example you should use an object pool, and reuse game objects, ins$$anonymous$$d of creating/destroying them in mass. Otherwise you'll end up with major performance issues, since the garbage collector has to work in overtime, cleaning up all those notes and particle systems.
I wont go into more detail here, but i hope i'll see you on the forums, where we can help you in a much better environment.
Answer by shadowpuppet · Mar 28, 2017 at 08:11 PM
I am no expert ( putting it mildly) so not sure I understand the setup. If I were to attempt this project what you have as a rigidbody on he rail I would have be a triggerzone(with your script on it) and the rigidbody and collider on the dropping note. SO when the note enters the triggerzone and the correct key pressed the corresponding prefab instantiates
Your answer
Follow this Question
Related Questions
Reinitialize prefab 0 Answers
How to save/load references to components and prefabs 1 Answer
Determine if game object is tyoepf One Prefab 3 Answers
Collision Detection for a Prefab? 3 Answers
How can i access in a prefab a variable from a gameObject? 1 Answer