- Home /
the sound will not play
Hi, please guys, i am frustrated, i work over 3 days... i have two objects (rocket and asteroids) every objects have script destruction-collision Code: public class DestroyByContact2 : MonoBehaviour {
public GameObject explosion;
void OnCollisionEnter()
{
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject);
Destroy(expl, 2); // (expl, delete the explosion after... seconds)
}
}
and now, i have problem create sound explosion...i created new script with name "Sound" and i inserted his code in to Rocket, his code Code: public class Sound : MonoBehaviour { public AudioClip impact; public AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Asteroids")
{
audioSource.PlayOneShot(impact, 0.7F);
}
}
} i have something bad? My goal is, when rocket crash into asteroids so it is created explosion with sound explosion.
Hey, i tried to answer your question with the switching scenes / explosion / delay problem but the question was gone before i could post my comment... idk why...
I don't know, it was probably blocked because of duplicity, although I didn't find anything like that ...
If i remember correctly you were trying to check an objects existence in its update method although you've already destroyd it through a collision check. This doesn't work.
You want an object that doesn't exist anymore to check if it exists or not. That's kind of paradox.
I obviously can't see your code anymore, just post it here again and i'll try to help.
Answer by xxmariofer · Mar 11, 2019 at 04:46 PM
probably the asteroids are far away from thje camera and the sound is not being heared? try this code
AudioSource.PlayClipAtPoint(impact, new Vector3(0, 0, Camera.main.transform.position));
Answer by CCnockaert · Mar 14, 2019 at 03:46 PM
Destroy(gameObject);
Destroy(expl, 2); // (expl, delete the explosion after... seconds)
Nothing to do with your problem but if you destroy this.gameObject then it'll never Destroy the expl gameObject.
Your answer
Follow this Question
Related Questions
How can I make a sound deploy after colliding with a certain object (or terrain)? 1 Answer
How do i prevent the player from dragging a ui element off screen? 1 Answer
Is possible to use UISprite object of NGUI with event type 2D to detect intersection? 0 Answers
Dice roll collision sound 1 Answer
Sphere (player) flattens/deforms when going on a moving platform. 0 Answers