Search Unity

Destroying Rigidbody Clones?

Discussion in 'Scripting' started by Doomer022, May 31, 2018.

  1. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Okay so i was working on a tank gun script. It creates a clone of the shell, gives velocity to it, then changes around some integers and plays some sound effects to make it feel better. However, i dont know how to remove the clones from the scene. I know that i can do it with "Destroy(What to destroy, Time)" but it simply wont allow me to use it. Any ideas how to fix this?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;
    using UnityEngine.UI;

    public class TankShell : MonoBehaviour {

    public AudioSource gunSound;
    public AudioSource reloadSound;
    public Rigidbody projectile;
    private Rigidbody projectileClone;
    public int shellVelocity = 60;
    public int shellClipSize = 1;
    public int totalShells = 25;
    public int reloadAmount = 1;
    public Text ammo;
    public Text ammoLoaded;
    public Color ammoColor;
    //public ParticleSystem cannonFlash;

    void Start()
    {
    projectileClone = GetComponent<Rigidbody>();
    ammoLoaded.color = Color.green;
    }
    void Update () {
    Fire();
    AmmoText();
    }
    public void Fire() {
    if (shellClipSize > 0) {
    if (Input.GetButtonDown("Fire1"))
    {
    projectileClone = Instantiate(projectile, transform.position, Quaternion.identity) as Rigidbody;
    projectileClone.velocity = transform.up * shellVelocity;
    Destroy(projectileClone, 3);
    gunSound.Play();
    //cannonFlash.Play();
    ammo.text = "Ammo Left: " + totalShells;
    shellClipSize -= 1;
    Reload();
    }
    }
    }
    public void Reload() {
    if (shellClipSize < 1 && totalShells != 0)
    {
    StartCoroutine("ReloadTime");
    }
    else {
    ammoLoaded.color = Color.red;
    ammoLoaded.text = "Out Of Shells!";
    }
    }
    public IEnumerator ReloadTime() {
    ammoLoaded.color = Color.yellow;
    ammoLoaded.text = "Reloading";
    yield return new WaitForSeconds(5);
    shellClipSize = shellClipSize + 1;
    totalShells = totalShells - 1;
    reloadSound.Play();
    ammoLoaded.color = Color.green;
    ammoLoaded.text = "Ready To Fire!";
    }
    public void AmmoText()
    {
    ammo.text = "Ammo Left: " + totalShells;
    }
    }
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    USE CODE TAGS.

    And in your code projectile and projectileclone are rigidbodies, not gameobjects. Shouldn't you be using remove component, then?
     
  3. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Okay fixed it, but because i cant add velocity to my shell, because it will only let me do it with a rigidbody, it just falls down, tho its gets dleted after time, which is good. So how do i add force to it?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    How? What did you do? :)

    How is it falling down if it does not have a RigidBody?

    Forces are added via a RigidBody.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm sure it has a Rigidbody; he's just not accessing it because he's changed the type of his variables to GameObject.

    I'd recommend going back to the original version, where your variables are of type Rigidbody. It's just that when you do the destroy, you need to do that on the GameObject, which is easily accessed because every component has a .gameObject accessor.

    Code (CSharp):
    1. projectileClone = Instantiate(projectile, transform.position, Quaternion.identity) as Rigidbody;
    2. projectileClone.velocity = transform.up * shellVelocity;
    3. Destroy(projectileClone.gameObject, 3);
     
    Doug_B likes this.
  6. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Hey Doug! Yes it does have a rigidbody, adding anything to the code wont do anything tho. Joe ill try your method out real soon! :)
     
  7. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Okay, worked perfectly! Thanks! :D
     
    Doug_B and JoeStrout like this.