- Home /
Emit particles only for clicked instance of an object
Hi, I'm trying to create a script to emit particles when I click on an object ("bomb") with a specific tag("bomb"). The problem is that I have many of this objects and when I click on one of them, instance of particle is created on all of them, instead i'd want to emit particles only on the clicked object.
Where is my error?
var explosion : Transform;
var timeOut : float = 5.0;
static var bomb : GameObject;
function Update(){
bomb = GameObject.FindGameObjectWithTag("bomb");
MouseClick();
}
function MouseClick(){
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "bomb") {
var instance = Instantiate(explosion, bomb.transform.position, bomb.transform.rotation);
Destroy(instance.gameObject, timeOut);
Destroy(hit.collider.gameObject);
}
}
}
}
Answer by thomasindustry · Apr 09, 2014 at 09:11 PM
Instead of looking for your bomb reference in the update loop using FindGameObjectWithTag, you should get the bomb component from your raycast hit after the physics check.
mmh...yes...but I've objects with the same tag randomly generated...how can i detect clicks on every single object?
Your answer

Follow this Question
Related Questions
Raycast with particle emitter, does it work? 0 Answers
raycasting prefab instances 0 Answers
Is there a way to draw particles on a raycast? 2 Answers
Hold on Collider 1 Answer