- Home /
Grenade Object Destruction
So, the problem is my grenade object is not being destroyed after a set time and the particle associated with the explosion effect. I have two scripts, one(Grenade.cs) assigned to the player where the grenade is instantiated and translated forward.
The second script(ExploGren.cs) is attached to the grenade prefab, here the particle is instantiated and the damage is dealt to all NPCs in the damage radius. After 5 seconds pass.
But what happens is the grenade is still in the game world and the explosion keeps refreshing and continuously detonating.
public class Grenade : MonoBehaviour
{
private float force = 20.0f;//players toss
public GameObject grenade;//its only destroying the rigidbody not the object
public Transform player;//where to spawn
int GAmount = 3;
public Font Tinker;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.G))
{
if(GAmount > 0)
{
Rigidbody spawnG = (Rigidbody)Instantiate(grenade, player.position, player.rotation);
spawnG.AddTorque(5,10,0);
spawnG.AddForce(transform.forward * force);
GAmount--;
Destroy(spawnG, 0.5f);
}
}
}
}
public class ExploGren : MonoBehaviour
{
public GameObject explosivo;//the explosive particle
private GameObject insta;// instatntiation of variables for explosion
private float timer = 0.0f;//zero time
private float timeTD = 5.0f;//time ot detonate
private float aliveTime = 0.05f;
private int exploDamage = 50;//grenade damage
private float explosiveRadius = 25.0f;//radious of the explosive damage
Collider[] collidersInRange;
Vector3 grenadeOrigin;
Health damageByGrenadeExplosion;//decalre health
// Use this for initialization
void Start ()
{
grenadeOrigin = transform.position;
}
// Update is called once per frame
void Update ()
{
timer +=1 * Time.deltaTime;//start counting
if(timer >= timeTD)//if timer is equal to detonation, detonate
{
collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
foreach(Collider col in collidersInRange)
{
damageByGrenadeExplosion = col.GetComponent<Health>();
if(damageByGrenadeExplosion != null)
damageByGrenadeExplosion.Damage(exploDamage);
}
}
Destroy(insta, aliveTime);
}
}
Answer by Saad_Khan · Jun 01, 2015 at 08:30 AM
In the second script im not sure where you are reseting the "timer" variable, although for starters you should put a null check before instantiating or destroying these objects in both scripts..
if(timer >= timeTD)//if timer is equal to detonation, detonate
{
if(insta==null)
insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
if(insta!=null)
Destroy(insta, aliveTime);
}
Answer by Irin · Jun 04, 2015 at 08:21 AM
The particle destruction for the explosion line i recycled from a muzzle flash script. I basically let the particle exist for .05f of a second. I have the time to detonate, and when it does it only lasts for .05f.
Okay i reset as you said, it worked well, it detonate, applys damage, except, with that restart code it still keeps detonating with a second or so resting period in between each burst. I think the situation can be remedied if i destroy the grenade model, the drawback is Unity wont let me destroy game objects.
Got it. The solution was simple and i feel stupid for not seeing it. So in the explogren.cs under Update i added the following lines: void Update () { timer +=1 * Time.deltaTime;//start counting
if(timer >= timeTD)//if timer is equal to detonation, detonate
{
collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
foreach(Collider col in collidersInRange)
{
damageByGrenadeExplosion = col.GetComponent<Health>();
if(damageByGrenadeExplosion != null)
damageByGrenadeExplosion.Damage(exploDamage);
}
}
Destroy (insta, aliveTime);
Destroy (gameObject, 5.0f);
}
Ill edit this code some more and add documentation, ill post the current one here and on my blog: novicegamedesign.blogspot.com
Thanks for the help Saad_Khan
public class ExploGren : MonoBehaviour
{
public GameObject explosivo;//the explosive particle
private GameObject insta;// instatntiation of variables for explosion
private float timer = 0.0f;//zero time
private float timeTD = 5.0f;//time ot detonate
private float aliveTime = 0.9f;
private int exploDamage = 50;//grenade damage
private float explosiveRadius = 25.0f;//radious of the explosive damage
Collider[] collidersInRange;
Vector3 grenadeOrigin;
Health damageByGrenadeExplosion;//decalre health
// Use this for initialization
void Start ()
{
grenadeOrigin = transform.position;
}
// Update is called once per frame
void Update ()
{
timer +=1 * Time.deltaTime;//start counting
if(timer >= timeTD)//if timer is equal to detonation, detonate
{
collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
foreach(Collider col in collidersInRange)
{
damageByGrenadeExplosion = col.GetComponent<Health>();
if(damageByGrenadeExplosion != null)
damageByGrenadeExplosion.Damage(exploDamage);
}
}
Destroy (insta, aliveTime);
Destroy (gameObject, 5.0f);
}
}
Make sure to attach the above to a grenade prefab ^ Its missing an audio source btw.
public class Grenade : MonoBehaviour
{
private float force = 20.0f;//players toss
public GameObject grenade;//its only destroying the rigidbody not the object
public Transform player;//where to spawn
int GAmount = 3;
public Font Tinker;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.G))
{
if(GAmount > 0)
{
Rigidbody spawnG = (Rigidbody)Instantiate(grenade, player.position, player.rotation);
spawnG.AddTorque(5,10,0);
spawnG.AddForce(transform.forward * force);
GAmount--;
}
}
}
void OnGUI()
{
GUIStyle TinkerFont = new GUIStyle();
TinkerFont.font = Tinker;//this works, changes the hud to Tinker Font.
TinkerFont.fontSize = 21;
TinkerFont.normal.textColor = Color.red;
GUI.Label(new Rect(890,520, 75,35),Value(GAmount), TinkerFont);
}
public string Value(int a)
{
string total;
return total = "GND: " + GAmount;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613200508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why doesn't my grenade go anywere 4 Answers
Null reference and GetComponent 1 Answer
Issues NPC Grenade avoidance 3 Answers
Character Controller Grenade problems 1 Answer
How do create a grenade launches? 0 Answers