PreFab Issues on Collision
I've been looking all over the boards for an answer and so far haven't been able to find one that is totally relevant or works. I'm super new to Unity 5 (and C#).
Here's what I'm trying to do: When one object collides with another, the max hit object collided with is destroyed, but before it is destroyed I want it to play a prefab particle system explosion.
using UnityEngine;
using System.Collections;
public class Nom : MonoBehaviour {
public AudioClip uh;
public ParticleSystem Explode;
public int maxHits;
private int timesHit;
void Start () {
timesHit = 0;
GetComponent<AudioSource> ().playOnAwake = false;
GetComponent<AudioSource> ().clip = uh;
}
void OnCollisionEnter2D (Collision2D col) {
//to play audio when hit
AudioSource audio = GetComponent<AudioSource>();
audio.Play ();
timesHit++;
//to determine when collided object is destroyed
if (timesHit >= maxHits) {
//what I thought would execute the particle system
GetComponent<ParticleSystem> ().playOnAwake = Explode;
Destroy(gameObject);
}
}
}
The particle explosion (named "Explode") is a prefab attached to the game object (I think--I dragged it so it's under the game object in the hierarchy).
Any thoughts? Thanks!
Your answer
Follow this Question
Related Questions
Use the particles System to Emit 2D gameObjects , How ? 2 Answers
How do I make this script play a particle sytem from prefab 1 Answer
For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers
Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers