- Home /
I need to destroy an instantiated prefab
My script goes as follows
private void Explode() { Collider[] thingsWeHit = Physics.OverlapSphere(transform.position, explosionRadius, explosionDetectionMask);
for(int i = 0; i < thingsWeHit.Length; i++)
{
Rigidbody hitRigidbody = thingsWeHit[i].rigidbody;
if(hitRigidbody != null)
{
hitRigidbody.AddExplosionForce(explosionForce,
transform.position,
explosionRadius,1f,ForceMode.Impulse);
if(hitRigidbody.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
Destroy(hitRigidbody.gameObject, .75f);
}
else if(hitRigidbody.gameObject == null)
{
Destroy(this.gameObject, 2f);
}
}
}
I want it to destroy itself after 2 seconds if it doesnt collide with any rigidbody so it doesnt fly on forever. Any help would be appreciated
I think the problem is that you check that hitRigidbody is not null at line 4 and then at line 14 you check if hitRigidbody.gameObject is null, so if I'm not mistaken try this at line 14:
else if(hitRigidbody.gameObject != null)
or you can delete this if statement, not sure that it neccesary here.
I tried that, neither of them work :( and taking the if statement out actually added 9 errors, I assume because I was saying if its not null do this, else it's not null do something else
Answer by OSG · May 13, 2014 at 07:39 AM
Try this
private void Explode() {
Collider[] thingsWeHit = Physics.OverlapSphere(transform.position, explosionRadius, explosionDetectionMask);
for(int i = 0; i < thingsWeHit.Length; i++)
{
Rigidbody hitRigidbody = thingsWeHit[i].rigidbody;
if(hitRigidbody != null)
{
hitRigidbody.AddExplosionForce(explosionForce,
transform.position,
explosionRadius,1f,ForceMode.Impulse);
if(hitRigidbody.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
Destroy(hitRigidbody.gameObject, .75f);
}
}
}
if(thingsWeHit.length == 0)
{
Destroy(this.gameObject, 2f);
}
}
--EDIT-- This code will destroy your object when u call explode function and an abject doesn't hit any enemy at this moment
Assets/Scripts/Scriptsfromclass/FireballProjectile.cs(31,49): error CS1061: Type UnityEngine.Collider[]' does not contain a definition for
length' and no extension method length' of type
UnityEngine.Collider[]' could be found (are you missing a using directive or an assembly reference?)
Thats the error Im getting when I copy and paste your code in. Using Length ins$$anonymous$$d of length gets rid of the error, but doesnt destroy the object.
This is the first script in full public class FireballProjectile : $$anonymous$$onoBehaviour { public Layer$$anonymous$$ask explosionDetection$$anonymous$$ask; public float explosionRadius; public float explosionForce; private static GameObject fireballProjectilePrefab;
void OnCollisionEnter(Collision collision)
{
Explode();
}
private void Explode() {
Collider[] thingsWeHit = Physics.OverlapSphere (transform.position, explosionRadius, explosionDetection$$anonymous$$ask);
for (int i = 0; i < thingsWeHit.Length; i++) {
Rigidbody hitRigidbody = thingsWeHit [i].rigidbody;
if (hitRigidbody != null) {
hitRigidbody.AddExplosionForce (explosionForce,
transform.position,
explosionRadius, 1f, Force$$anonymous$$ode.Impulse);
if (hitRigidbody.gameObject.layer == Layer$$anonymous$$ask.NameToLayer ("Enemy")) {
Destroy (hitRigidbody.gameObject, .75f);
}
}
}
if (thingsWeHit.length == 0) {
Destroy (this.gameObject, 2f);
}
particleSystem.Play ();
rigidbody.is$$anonymous$$inematic = false;
collider.enabled = false;
renderer.enabled = false;
Destroy (gameObject, 1f);
}
}
}
Answer by Ideas · May 13, 2014 at 07:53 AM
why not just put
void Start()
{
Invoke("SomeMethod", 2.0f);
}
void SomeMethod()
{
Destroy(gameObject); //if you want the script to be destroyed
// put 'Destroy(this);' instead
}
anywhere in your script pretty much. This should get the job done.
Also did not work for me, the fireballs still remain in the hierarchy and are never destroyed
Put the code in its own script(meaning its very own file)
$$anonymous$$ake sure its attached to the prefab in the Project view so that when it get instantiated it will already be attached
No matter what happens the gameObject this script is attached to will be destroyed after 2 secs
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Solitaire Game Source Code to Play With 0 Answers
A newbie to unity.Working on a game,Need HELP !!?! 2 Answers
I'm new to game development. What do I need to learn? 3 Answers
Blender world to Unity? Good Idea? 1 Answer