Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to destroy bullet clone

Discussion in 'Scripting' started by valkarminator, Apr 15, 2016.

  1. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    Hello,

    I spend 1 hour on that and I didn't find/make a script to destroy ONLY bullet clone,
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8. Destroy (this.gameObject, 5);
    9. }

    The problem with this code destroy my entire script, and then, I can't fire with gun.

    Thank you in advance.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    1 way would be to store the current bullet clones in a list and then just destroy a specific one
    You could also give the bullet the script, that destroy it using GameObject.Destroy(gameObject) (don't use this.)
    If you want ot destroy the bullet after a specific time, you could the first two method with a counter oooor, you could set up a coroutine, that takes in a bullet and then after a specified time (yield WaitForSeconds(5) in US) destroy it

    -------------AVOID AT ALL COSTS----------------USE ONLY IF NECESSARY----------
    GameObject.Find ("Bullet(Clone)") works
    Give it a tag right after instantiation and then GameObject.FindGameObjectWithTag
    -----------------------------------------------------------------------------------------------------------
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1. function FireBullet() {
    2.     var bullet : GameObject = (GameObject) Instantiate(TheBulletPrefab); // spawn bullet instance
    3.     Destroy(bullet, 5); // Destroy bullet instance after 5 seconds
    4. }
    Or add this script to your bullet
    Code (csharp):
    1. function Awake() {
    2.     Destroy(gameObject, 5);
    3. }
    Your original bullet should be a prefab in the Project Panel, not in the scene.
     
    EwnT9 and brunosantosdesousa0705 like this.
  4. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    thank all of you for your reply,

    I find this, it is a little old
    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. var projectile : Rigidbody;
    5. var speed = 20;
    6.  
    7. function Update () {
    8. if(Input.GetButton("Fire1"))
    9. {
    10. clone = instantiate(projectile, transform.position, transform.rotation);
    11. clone.velocity = transform.TransformDirection(Vector3(0,0, speed));
    12. Destroy (clone.gameObject, 3);
    13. }
    14. }
    15.  

    In fact, I used a gameObject whereas we have to do use a prefab, I didn't know that.

    With this code, I have a error BCE 0005, he didn't know clone, how can I fix that ?

    Thx in advance
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    At first glance
    1. You didn't declare the clone variable (var clone : Rigidbody)
    2. Instantiate should be capitalized
     
  6. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    I rectify it, but, like you say, I have a other error BCE 0005 : unknow : 'instantiate'

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. var projectile : Rigidbody;
    5. var clone : Rigidbody;
    6. var speed = 20;
    7.  
    8. function Update () {
    9. if(Input.GetButton("Fire1"))
    10. {
    11. clone = instantiate(projectile, transform.position, transform.rotation);
    12. clone.velocity = transform.TransformDirection(Vector3(0,0, speed));
    13. Destroy (clone.gameObject, 3);
    14. }
    15. }
    16.  
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I already gave you the solution in the previous post :)

    It is case sensitive, it should be capitalized. Instantiate

    I would also declare the clone var inside of Update, you won't be using it anywhere else.
     
  8. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var projectile : Rigidbody;
    4. var speed = 20;
    5.  
    6.  
    7. function Update () {
    8. var clone : Rigidbody;
    9. if(Input.GetButton("Fire1"))
    10. {
    11.  
    12. clone = instantiate(projectile, transform.position, transform.rotation);
    13. clone.velocity = transform.TransformDirection(Vector3(0,0, speed));
    14. projectile =(GameObject) Instantiate(clone); // I include your fonction FireBullet
    15. Destroy(projectile, 5);
    16.  
    17. }
    18. }
    19. [/code
     
  9. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    I have a other error UCE 0001 ';' (13,25)excepted, I don't understand it, where is my error ?
     
  10. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I recommend you take a stroll through the learn section: http://unity3d.com/learn

    These are pretty basic errors best solved through the use of tutorials.
     
  11. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    I will see it, thank you for your replies ;)
     
  12. valkarminator

    valkarminator

    Joined:
    Apr 15, 2016
    Posts:
    7
    Since yesterday, I tried to find a solution and now, it's resolve now :

    I had just to put this script :
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8. Destroy (this.gameObject, 5);
    9. }
    10.  
    On bullet fab, and it's work !!
     
  13. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    If using that method (as in - self destruct script as a separate component), you may want to move the Destroy call to Start (or OnEnable, or Awake). Right now it will get called every frame, wasting resources.

    Don't put more into Update than you want to get called every single frame, it will bite back very soon. Especially with things like bullets that can appear in large numbers.
     
  14. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    PRO TIP:
    Don't use prefabs as a bullet
    Why?
    Unity runs physics checks every 1/x seconds (where x is the amount of checks in a sec). If a bullet is really fast, like normal bullets, th bullet will move trough walls, enemies, players, prefabs etc. and it will most likely skip the detection, so your bullet will not hit. If your bullet is slow, so it can hit the obstacle, it will fall to the ground pretty easily and it will look kinda pathetic.
    How do you solve this:
    In reality bullets move... (quick google search) 1200 m/s (YES, i will use the metric system). If the ground is completely flat and a human holds the gun at around 1.5 m in height, then (calculating) the bullet will travel 0.54 * 1200 = 648 meters before falling on the ground. This is a lot if you're not making the new "Skyrim with guns". So gravity with bullets is not a thing that really matters (and who the f*ck wants to calculate the difference before shooting anyways?) So the best solution is to use raycast and just add a particle effect to the muzzle for the show
     
  15. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I won't tell you one way or the other on how you should do your bullets, but @gorbit99 your info on why not to use physics is out of date as of Unity5. Refer to this thread:

    http://forum.unity3d.com/threads/hi...vs-projectile-simulation.334463/#post-2188153

    my post in that thread shows a very basic weapon with a physics bullet with a velocity set to 1000:

    https://dl.dropboxusercontent.com/u/1475775/UnityForumsStuff/RigidTest/RigidTest.html (webplayer build so IE only I guess since all other browsers dropped plugins)
     
    VitruvianStickFigure likes this.