Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lotoos · Feb 07, 2017 at 03:37 PM · c#scripting problemgameobjectdestroybullet

The bullet never destroyed!!!

Hello guys, i have a problem with my bullet. I tried to do that bullet destroy themselves but failed. bullet did not destroy. Do not know why .. btw I'm learning please help me. I want to add it to my atack script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Atack : MonoBehaviour
 {
 
     public GameObject bullet;
     public float view = 100.0f;
     public float wait = 1.0f; // how much time wait for next shot
     public float counttheshot = 1.0f; // count to the next shot
     public float damage = 50.0f;
     public float timeoflife = 2.0f; // time of life bullet
 
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (counttheshot < wait)
             counttheshot += Time.deltaTime;
 
         if (Input.GetMouseButton(0) && counttheshot >= wait)
         {
             counttheshot = 0;
 
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit hitInfo;
 
             if (Physics.Raycast(ray, out hitInfo, view))
             {
                 Vector3 hitPoint = hitInfo.point;
                 GameObject go = hitInfo.collider.gameObject;
 
                 hit(go);
 
                 if (bullet != null)
                     Instantiate(bullet, hitPoint, Quaternion.identity);
 
             }
         }
         // there is it
         timeoflife -= Time.deltaTime;
 
         GameObject cloneBullet = Instantiate(bullet);
         if (timeoflife <= 0)
             Destroy(cloneBullet, timeoflife);
 
     }
 }
 

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 Chikari · Feb 07, 2017 at 05:01 PM 0
Share

Not sure what happens when you call Destroy(cloneBullet, timeoflife) with a potential negative value for timeoflife. What happens when you replace Destroy(cloneBullet, timeoflife) with Destroy(cloneBullet) ?

avatar image Lotoos · Feb 08, 2017 at 11:05 AM 0
Share

Same.. Just nothing happend. timeoflife going to $$anonymous$$us values. Bullet never destroy. Damn..

2 Replies

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

Answer by Salmjak · Feb 08, 2017 at 12:36 PM

                  if (bullet != null)
                      Instantiate(bullet, hitPoint, Quaternion.identity);

You never pass a reference to this object. This is what you want to destroy.

 GameObject cloneBullet = Instantiate(bullet);
          if (timeoflife <= 0)
              Destroy(cloneBullet, timeoflife);

This only creates an object and then destroys it (if timeoflife

You need to have your bullet-reference outside of the Update()-loop.

 // Update is called once per frame
 GameObject myBulletCopy = null;
      void Update()
      {
          if (counttheshot < wait)
              counttheshot += Time.deltaTime;
  
          if (Input.GetMouseButton(0) && counttheshot >= wait)
          {
              counttheshot = 0;
  
              Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
              RaycastHit hitInfo;
  
              if (Physics.Raycast(ray, out hitInfo, view))
              {
                  Vector3 hitPoint = hitInfo.point;
                  GameObject go = hitInfo.collider.gameObject;
  
                  hit(go);
  
                  if (bullet != null && myBulletCopy == null)
                      myBulletCopy = Instantiate(bullet, hitPoint, Quaternion.identity);
  
              }
          }
          // there is it
          timeoflife -= Time.deltaTime;
          if (timeoflife <= 0)
              Destroy(myBulletCopy);
              myBulletCopy = null;
              timeoflife = 2.0f;
  
      }
  }

If you want more than one bullet you will have to start working with lists or arrays or even better, a custom Bullet-class which handles destruction. Something like:

 public class Bullet : Monobehaviour{
 
 public float lifeTime = 2.0f;
 
 void Update()
 {
 lifeTime -= Time.deltaTime;
 if(lifeTime <= 0){
 Destroy(this.gameobject);
 }
 }
 
 }

Add it to your bulletPrefab.

Comment
Add comment · 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
2

Answer by ASPePeX · Feb 08, 2017 at 02:42 PM

I boiled your code down to work with the essential thing you are asking about.

 using UnityEngine;
 
 public class Atack : MonoBehaviour
 {
     public GameObject bulletPrefab;
 
     public float shotCooldown = 1.0f; // how much time wait for next shot
     private float shotTimer; // count to the next shot
     public float timeoflife = 2.0f; // time of life bullet
 
     void Update()
     {
         if (shotTimer <= 0)
         {
             if (Input.GetMouseButton(0))
             {
                 shotTimer = shotCooldown;
 
                 //Instatiating with reference
                 GameObject bullet = Instantiate(bulletPrefab, Vector3.zero, Quaternion.identity);
                 Destroy(bullet, timeoflife);
             }
         }
         else
         {
             shotTimer -= Time.deltaTime;
         }
     }
 }

I'm doing the cooldown for shooting a bit different, setting it and counting down until it skips over zero. But this is just my preference how to do this. The essential part is instantiating with a reference and passing the reference to Destroy(). Also the second attribute you pass to Destroy() already does everything for you, no need for calculating the delay yourself ("Fire and forget" haha cough).

Comment
Add comment · 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

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

328 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroyed instance of Prefab, can't spawn it back. 1 Answer

Write values on an instantiated Gameobject ? 1 Answer

Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers

ScriptName versus PingPongScriptName? 0 Answers

How to instantiate a game object every time when space key is pressed 2 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