Problem with enum

I have checked the code, rewritten it and even copied from the source but I get an error when loading. ‘Unexpected symbol enum’ followed by errors for each part of the line to the end.

The script starts -
using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour {

[SerializeField] float rcsThrust = 250f;
[SerializeField] float mainThrust = 500f;
Rigidbody rigidBody;
AudioSource audioSource;
// Use this for initialization
void Start () {
	Rigidbody rigidBody;
	AudioSource audioSource;

	enum State { Alive, Dying, Transcending }
	State state = State.Alive;
}

which as far as I can see is the same as the course one, except of course it does not load.

I’m not on the Unity 2.0 course so can’t check the lecture but are you sure that the enum declaration is supposed to be inside the start method? i’ve only ever came across them declared at the top of the class itself. You also seem to be declaring rigidBody and audioSource twice both at the class level and inside the start method. I think this is what you would need instead:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour {

    [SerializeField] float rcsThrust = 250f;
    [SerializeField] float mainThrust = 500f;
    enum State { Alive, Dying, Transcending };

    Rigidbody rigidBody;
    AudioSource audioSource;
    State state = State.Alive;

    void Start() {

    }
}
1 Like

Thanks a lot, I think I went code blind there. What was probably a simple error at first was made worse trying to fix it. Must get more sleeep.

1 Like

Nah just need more coffee, sleep is for the weak :wink:

1 Like

I’ve caught myself forgetting to put Enums outside of the class body, and I’ve been coding for quite a while.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms