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 /
  • Help Room /
avatar image
0
Question by cloudnine1098 · Mar 08, 2013 at 04:21 AM · rigidbodyinstantiatedestroybulletclone

Bullet clones will not be destroyed.

So i've been working on this mechanic for some time and finally got it to work, I just need the clones to destroy themselves after a few seconds (some bullets will run of into space). Here is my code, thanks!

 using UnityEngine;
 using System.Collections;
 
 public class Turret3 : MonoBehaviour {
     
     public GameObject bullet;
     public GameObject cannonFlash;
     
     private GameObject spawnPt;
 
     // Use this for initialization
     void Start () {
         
         spawnPt = GameObject.Find ("oneSpawn");
         
         cannonFlash.SetActive(false);
     
     }
     
     // Update is called once per frame
     void Update () {
     
          Vector3 fwd = transform.TransformDirection(Vector3.forward);
         
     if (Input.GetButtonDown ("Fire1")) 
         
         {
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             
         RaycastHit Hit;
             
          if (Physics.Raycast(ray, out Hit, 1000))
                 
         {
                     
             Debug.DrawLine(ray.origin, Hit.point);
             
             GameObject newProjectile = Instantiate (bullet, spawnPt.transform.position, transform.rotation)as GameObject;
                     
             newProjectile.transform.LookAt (Hit.point);
                     
             newProjectile.rigidbody.AddRelativeForce(Vector3.up + Vector3.forward * 5000);
                     
             Destroy (newProjectile, 5);
                 
                 print("" + Hit.point);
         }
             
                     cannonFlash.SetActive(true);
 
             
         }
         
         if(Input.GetButtonUp ("Fire1"))
             
         {
             cannonFlash.SetActive(false);
 
         }
     }
 }
 
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 robertbu · Mar 08, 2013 at 04:37 AM 0
Share

Do you mean that line 45 is not working for all of your projectiles?

avatar image rakkarage · Mar 22, 2014 at 01:14 AM 0
Share

you might want to consider pooling? because creating and destroying so many objects can harm performance on mobiles sometimes i heard? here is an example of pooling bullets so it recycles them ins$$anonymous$$d of deleting them https://github.com/strangeioc/strangerocks

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tasarran · Mar 08, 2013 at 09:42 AM

Try putting a simple script on the bullet Prefab that only allows it to exist for a set time...

In other words, a script with a timer, that when it runs out, Destroy()s the parent GameObject.

If you need me to, I will write the script.

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 pulkit8.mahajan · Mar 22, 2014 at 01:03 AM 0
Share

Hi, I have been searching for a long time. I have a function called fire, that shoots the bullets, and I want to destroy them if they pass out of view. But for some reason after it gets destroyed and i try to shoot again, it sends an error saying that the object is already destroyed. How do I fix this?

avatar image
0

Answer by corriedotdev · Mar 23, 2014 at 10:00 PM

     var lifeTime = 5.0; 
 
     function Awake ()
     {
     Destroy(gameObject, lifeTime);
     }

Attach to the bullet, should only stay in the scene for 5 seconds here. Change the value as required ;)

Comment
Add comment · Show 2 · 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 arthurptj · Nov 15, 2015 at 06:07 PM 0
Share

bad deletes game object not clone

avatar image corriedotdev arthurptj · Aug 05, 2016 at 08:19 AM 0
Share

It wont, its attached to the prefab which is a clone.. therefore would be deleted

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

14 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

Related Questions

destroy asset is not permitted to avoid data loss? 0 Answers

Cannot destroy the SphereCollider on Instantiated Objects 1 Answer

Script removing rigidbody component, NOT game object 1 Answer

I need help destroying a clone on function. 1 Answer

Destroy the first instantiated clone after 4 have been instantiated 1 Answer


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