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; } }
USE CODE TAGS. And in your code projectile and projectileclone are rigidbodies, not gameobjects. Shouldn't you be using remove component, then?
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?
How? What did you do? How is it falling down if it does not have a RigidBody? Forces are added via a RigidBody.
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): projectileClone = Instantiate(projectile, transform.position, Quaternion.identity) as Rigidbody; projectileClone.velocity = transform.up * shellVelocity; Destroy(projectileClone.gameObject, 3);
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!