- Home /
Help With Remote Explosive
/I USE JAVASCRIPT\
Hey I'm doing a little practice project. The basic concept is some C4, in my scripts I call it a grenadeProp and the button that detonates it, the grenadeRemoteProp
(prop because they're the objects the player holds, the thrown grenade is just called grenade or grenadePrefab)
So I have almost everything working for the remotely detonated grenade, let's just call it C4 for easy reference. I have the animations, and I can get the grenadePrefab to spawn. But it doesn't work quite right. I use a master script for the weapons FYI (FirePrimary is the Fire1 button) and (FireSecondery is the Fire2 button) that broadcast those button's inputs to the other scripts.
I'm trying to make the grenadePrefab explode when I broadcast the message FirePrimary using a detonate var from the grenadeRemoteProp's script and it should destroy the grenadPrefab, and instantiate an explosion.
So I've fiddled around with my scripts and I can either get get the grenade to destroy itself, but not spawn the explosion. Or get the grenadePrefab to spawn an explosion on itself and then instantiate itself every frame infinitely.
So I've reached a point where I can't find the answer anywhere (maybe I'm looking for the wrong keywords) and I need some help!
Here's the script for the Grenade Prefab:
var grenadeController:Transform;
var explosionPrefab:Transform;
function Update () {
var detonate = grenadeController.GetComponent(wGrenadeRemoteProp).detonate;
if(detonate >= 1)
Destroy(gameObject, 0.2f);
explosion = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity);
}
And here's the GrenadeRemoteProp's Script:
var Thrown = 0;
public var detonate = 0;
function FirePrimary () {
detonate += 1;
}
function FireSecondary () {
if(Thrown >= 1)
return;
if(Thrown < 0)
Thrown += 1;
animation.Play("GrenadeThrow02");
}
Whether you use Javascript or C# is irrelevant a helping hand is a helping hand no matter if it is left or right handed.
Answer by Chris D · Jul 14, 2011 at 06:08 PM
This is your problem:
if(detonate >= 1)
Destroy(gameObject, 0.2f);
explosion = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity);
You're destroying the gameObject (and the instance of this script) before it has a chance to instantiate the explosion.
Switch the order.
Your answer
Follow this Question
Related Questions
Variable Access Problem (Javascript) 3 Answers
OverlapSphere not causing damage? - Solved 2 Answers
timed bomb, explosion detection 2 Answers
Automatic fire in weapons 1 Answer
Cycling through enum with key press 1 Answer