Hi There,
I have a question on the GameObject.Destroy (NameOfObject); code.
I’ve been trying to remove the rocket at impact (or transform it to an animation at later stage). However, when doing the following tweaks to Ben’s Code it will not do anything (see commented sections for readability guidance):
Any suggestion how to destroy the object?
EDIT
Destroy(gameObject); is the code required to destroy the object. However it now stops the game immediately.
EDIT EDIT
GameObject.Find(“Rocket”).transform.localScale = new Vector3(0, 0, 0);
That one makes it as intended xD
public class FlyingInput : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource audioSource;
[SerializeField] float rcsThrust = 100F;
[SerializeField] float mainThrust = 100F;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip GameOver;
[SerializeField] AudioClip IdleThrust;
[SerializeField] AudioClip Success;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem successParticles;
[SerializeField] ParticleSystem deathParticles;
KeyCode SpaceKey;
GameObject Rocket; **// Calling out that the Rocket is here**
int sceneNumber;
enum State { Alive, Dying, Transcending, Idling }
State state = State.Alive;
public void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
public void OnCollisionEnter(Collision collision)
{
sceneNumber = SceneManager.GetActiveScene().buildIndex;
if (state != State.Alive) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
print("Friend");
break;
case "Final":
state = State.Transcending;
if (sceneNumber == 1)
{
Invoke("LoadCurrentScene", 1f);
}
else if (sceneNumber == 0)
{
Invoke("LoadNextScene", 1f);
}
break;
default:
state = State.Dying;
if (sceneNumber == 0)
{
deathParticles.Play();
GameObject.Destroy(Rocket); // Immediately after state is triggered and death particle activated, the object should be destroyed.
audioSource.Stop();
audioSource.PlayOneShot(GameOver);
Invoke("LoadCurrentScene", 4f);
}
else if (sceneNumber == 1) {
deathParticles.Play();
GameObject.Destroy(Rocket);
audioSource.Stop();
audioSource.PlayOneShot(GameOver);
{ Invoke("LoadNextScene", 4f); }
}
break;
}
}
The Rocket is named in the inspector