I’m using Unity 2019.2 and I’ve noticed several differences during the course when it comes to prefabs, so I’m hoping this error is related to that. When I run my rocket ship in level one, the particles work fine, but in level 2 they stop working. Here is a quick video:
I made sure to link the particle assets from the hierarchy and not the asset window, and I made sure that the particle prefabs were applied to the Rocket prefab and not a variant (this new prefab workflow was really confusing to learn, btw). Any help would be greatly appreciated.
Here is my code:
public class Rocket : MonoBehaviour
{
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
[SerializeField] float transcendTime = 1f;
[SerializeField] float deathTime = 1f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip death;
[SerializeField] AudioClip success;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem deathParticles;
[SerializeField] ParticleSystem successParticles;
Rigidbody rigidBody;
AudioSource audioSource;
enum State {Alive, Dying, Transcending};
State state = State.Alive;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
}
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
print("Ok");
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartDeathSequence()
{
audioSource.Stop();
state = State.Dying;
audioSource.PlayOneShot(death);
deathParticles.Play();
Invoke("LoadDeathScene", deathTime);
}
private void StartSuccessSequence()
{
audioSource.Stop();
state = State.Transcending;
audioSource.PlayOneShot(success);
successParticles.Play();
Invoke("LoadNextScene", transcendTime);
}
void LoadDeathScene()
{
SceneManager.LoadScene(0);
}
void LoadNextScene()
{
SceneManager.LoadScene(1);
}
private void RespondToRotateInput()
{
float rotationThisFrame = rcsThrust * Time.deltaTime;
rigidBody.freezeRotation = true; //take manual control of rotation
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false; //resume physics control of rotation
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
mainEngineParticles.Play();
}
}