- Home /
** why does my Enum parameter goes back to default value? **
i have created 4 levels, but i'm stuck in the first two because my enum Level keeps going back to default=level_1... so i'm stuck between level 1 and 2,why does my parameters goes back to initial/default value?, thanks . Btw this is true as well for any other parameters i've tried.
using UnityEngine;
using UnityEngine.SceneManagement;
[DisallowMultipleComponent]
public class Rocket : MonoBehaviour
{
[SerializeField] float rcsThrust = 300f;
[SerializeField] float mainThrust = 2000f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip death;
[SerializeField] AudioClip success;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem deathParticles;
[SerializeField] ParticleSystem successParticles;
[SerializeField] float levelLoadDelay = 2f;
Rigidbody rigidBody;
AudioSource audioSource;
enum Level { Level_1, Level_2, Level_3, Level_4 };
Level currentLevel = Level.Level_1;
enum State { Alive, Dying, Transcending };
State state = State.Alive;
void Start()// Start is called before the first frame update
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
}
print(currentLevel); //print check
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive)
return;
switch (collision.gameObject.tag)
{
case "Friendly":
//do nothing
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
successParticles.Play();
Invoke("LaodNextScene", levelLoadDelay);
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
deathParticles.Play();
Invoke("LaodFirstScene", levelLoadDelay);
}
private void LaodNextScene()
{
switch (currentLevel)
{
case Level.Level_1:
{
print("loading level 2");
currentLevel = Level.Level_2;
SceneManager.LoadScene("Level_2");
break;
}
case Level.Level_2:
{
print("loading level 3");
currentLevel = Level.Level_3;
SceneManager.LoadScene("level_3");
break;
}
case Level.Level_3:
{
print("loading level 4");
currentLevel = Level.Level_4;
SceneManager.LoadScene("level_4");
break;
}
case Level.Level_4:
{
print("loading level 4 again");
SceneManager.LoadScene("level_4");
break;
}
default:
print("invalid level");
break;
}
}
private void LaodFirstScene()
{
currentLevel = Level.Level_2;
SceneManager.LoadScene(currentLevel.ToString()); //changed for debugging-should go to level_1
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
ApplyThrust();
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
if (!audioSource.isPlaying)
audioSource.PlayOneShot(mainEngine);
mainEngineParticles.Play();
if (!mainEngineParticles.isPlaying)
print("Error");
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true;
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false;
}
},
Answer by Vega4Life · Aug 03, 2019 at 01:00 PM
Because when you change scenes, it also recreates the rocket script - making it completely fresh. In other words, every time you change the scene, all your values get reset.
You need to extract out the changing of the scene into some sort of SceneManager that is static (or a singleton) that wont be destroyed on load. Then rocket script calls into the SceneManager to switch scenes. The rocket script will be reloaded, but the sceneManager maintains all the correct data.
Like someone else mentioned you could also just make Level currentLevel a static (static Level currentLevel) - and it would keep its value for a reload. I didn't mention it because instinctively didn't think the rocket should worry about handling reloading of the scene. But either way works.