- Home /
how to mark two objects and collision to explode and play the sound
Hi, how do I tag the second object in the code below? Code: public class DestroyByContact2 : MonoBehaviour {
public AudioClip impact;
AudioSource audioSource;
public GameObject explosion; // drag your explosion prefab here
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Asteroids" ) {
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
audioSource.PlayOneShot(impact, 0.7F);
Destroy(collision.gameObject); // destroy the grenade
Destroy(expl, 5); // delete the explosion after 2 seconds
}
}
}
Comment
Answer by Orami · Mar 09, 2019 at 08:59 PM
The game object with the script attached can be accessed with gameObject. The second game object can be accessed by using collision.gameObject
Your answer
![](https://koobas.hobune.stream/wayback/20220612193254im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My trigger is called twice 1 Answer
Incorrect velocity after collision 1 Answer
My game objects go into each others..How can i fix it? 1 Answer