Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by varunvp · Apr 09, 2015 at 04:35 AM · collisionrigidbodyobject pooldestroy-clones

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;
                 }
             }

alt text

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?

unity.png (488.8 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Apr 09, 2015 at 08:11 AM 0
Share

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

avatar image Nido · Apr 09, 2015 at 08:21 AM 1
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vivalavida · Feb 06, 2018 at 06:17 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges