- Home /
How do I use my particle prefab on collision with my brick?
Im new to programming so this is most likely a noob question but I was wondering how I would make my particle prefab run once it collides with a brick. I have found other sources online of how to do this but none of them seem to work for me. Does anyone know how I would do this?
private var score : int=0;
var guiScore : GUIText;
var blue : Texture;
var isBlue : boolean = false;
var brickPieces : GameObject;
function Start () {
guiScore.text="Score: 0";
}
function OnCollisionEnter(col : Collision){
if(col.collider.name == "Brick3"){
Destroy(col.gameObject);
//I want the brick to explode showing my particles here
brickPieces(col.gameObject);
score += 10;
guiScore.text= "Score: " + score;
renderer.material.mainTexture = blue;
isBlue = true;
}
Answer by nesis · Mar 06, 2014 at 01:57 PM
Is the GameObject variable called brickPieces your particle system? It's hard to tell for sure.
If that's the case, you can make accessing the particle system a little easier by changing that variable from being GameObject to being ParticleSystem. Then, when it comes time to start showing particles, you can use this:
brickParticles.Play();
This method will just need you to have the particle system's "Play On Awake" checkbox to be unticked in the Inspector, so it doesn't start playing the moment the scene starts / its GameObject activates.
I did everything you said and even that wont work.
set brickPieces to ParticleSystem
insert brickPieces.Play(); where I want it to deploy in the script
$$anonymous$$ade sure the particle systems Play On Awake checkbox was unchecked.
None of this seemed to work. I really thought it would to because it all made sense. Do you have any other suggestions to help me get this thing to work?
Even If I just put brickPieces.Play(); in the update method, it never once runs in the game. This is a serious problem. I have it as a Prefab but its not in the game object bar with all of my other objects. I have it like this because I only want it to run at certain instances.
Oh, so you don't have an instance of it? Use Instantiate(brickPieces,transform.position,transform.rotation) then. That'll take your prefab (which tells Unity what you want to create) and make an instance of it (which actually puts it in your game).
THAN$$anonymous$$ YOU! that totally worked. $$anonymous$$uch appreciated kind sir.
Your answer