- Home /
Missile GameObject being destroyed automatically!
Hey all!, I have used object pooling for some missile gameObjects. They are retrieved using an empty 'missile launcher' gameObject. But after firing one missile, it follows the target, strikes it, and gets destroyed. I have a line of code only to disable it, but not destroy it. The subsequent calls for retrieving the instance result in a NullReferenceException. I don't know how it is being destroyed. Here is the code for the missile,
using UnityEngine; using System.Collections;
public class Missile : MonoBehaviour
{
public float missileVelocity = 300;
public float turnSpeed = 20;
Rigidbody homingMissile;
float fuseDelay;
public Transform target;
Transform _transform;
public void Start()
{
_transform = transform;
homingMissile = _transform.rigidbody;
Debug.Log("Fire");
Fire();
}
void Fire()
{
//yield WaitForSec
var distance = Mathf.Infinity;
foreach(GameObject go in GameObject.FindGameObjectsWithTag("Dangerous"))
{
var diff = (go.transform.position - _transform.position).sqrMagnitude;
if(diff < distance)
{
distance = diff;
target = go.transform;
}
}
}
void Update()
{
if(target == null || homingMissile == null)
return;
homingMissile.velocity = _transform.forward * missileVelocity;
var targetRotation = Quaternion.LookRotation(target.position - _transform.position);
homingMissile.MoveRotation(Quaternion.RotateTowards(_transform.rotation, targetRotation, turnSpeed));
}
void OnCollisionEnter(Collision collision)
{
collision.collider.gameObject.SendMessageUpwards("TakeDamage",20,SendMessageOptions.DontRequireReceiver);
gameObject.SetActive(false);
}
}
the missile launcher,
public GameObject missile;
public float fireRate;
Crosshair crosshair;
private Pool pool;
void Start()
{
pool = GameObject.Find("MissilePool").GetComponent<Pool>();
// crosshair = GetComponent<Crosshair>();
}
// Update is called once per frame
public void Fire ()
{
missile = pool.RetrieveInstance();
Debug.Log(pool.name);
if(missile)
{
missile.transform.position = transform.position;
missile.transform.rotation = transform.rotation;
}
}
and the part of the script which calls the missile launcher.
missileLaunch = GetComponentInChildren<Missilelauncher>();
if(CrossPlatformInput.GetButton("Fire2"))
{
MissileTimer += Time.deltaTime;
if(MissileTimer > fireRate)
{
missileLaunch.Fire();
Debug.Log("Fire");
MissileTimer=0;
}
}
PS - The 'Brake" button is for firing a missile and 'jump' for the cannon.
The Missile prefab has a RigidBody on it. Could this be the cause of the problem?
Hello,
I am not sure about this behaviour but it could be that to unity, setting active to false or destroying the object throws the same null reference exception message. from your missile script remove this line : //gameObject.SetActive(false);
and see if you still get the error.
the error comes from pool.cs script it could be helpful to see it too :p
Sometimes I want to disable animated gameObjects temporarily. But if I do the SetActive(), the animation state restarts. So I did my own setActive() wich disable the renderers ins$$anonymous$$d of the gameObject. If you want to try it....
private Renderer[] renderers;
// Use this for initialization
public virtual void Awake ()
{
renderers = GetComponentsInChildren<Renderer>();
}
public void setActive(bool state)
{
foreach (Renderer rend in renderers)
{
rend.enabled = state;
}
}
And if needed, reset the object via code.
Edit: you don't need that "virtual" xD it's from my actual code
Answer by varunvp · Apr 17, 2015 at 12:17 PM
Hey guys, this was really embarassing. Turns out that the gameObject was being destroyed because I had a TrailRenderer attached to it with the 'Autodestruct' on. Turned it off.
hey I actually did the same thing. Was managing my bullets via a pooler which just deactivated the bullets, but they were somehow getting destroyed, which was very confusing.
Luckily I stumbled on this post.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to speed up? 3 Answers
Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer
Rigidbody Collision? 1 Answer
RigidBody Clings to Walls 0 Answers
Weird bounching with Rigidbodies (2D) 0 Answers