States doesn't show in inspector

Hello, I have a weird problem with the states not showing under the rocket script in the inspector. only the Thrust sliders show. It can’t be something with the script cus I tried to use the exact same as Ben, but it still doesn’t show.

It’s probably just me being stupid and overlooking something, but any kind of help would be appreciated.

Can you post the code where you’re defining the states?

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour {

    // todo fix lighting bug
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;

    Rigidbody rigidBody;
    AudioSource audioSource;

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

	// Use this for initialization
	void Start () {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
        // todo somewhere stop sound on death
        if (state == State.Alive)
        {
            Thrust();
            Rotate();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; } // ignore collisions when dead

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                // do nothing
                break;
            case "Finish":
                state = State.Transcending;
                Invoke("LoadNextLevel", 1f); // parameterise time
                break;
            default:
                print("Hit something deadly");
                state = State.Dying;
                Invoke("LoadFirstLevel", 1f); // parameterise time
                break;
        }
    }

    private void LoadNextLevel()
    {
        SceneManager.LoadScene(1); // todo allow for more than 2 levels
    }

    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);
    }

    private void Thrust()
    {
        if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
        {
            rigidBody.AddRelativeForce(Vector3.up * mainThrust);
            if (!audioSource.isPlaying) // so it doesn't layer
            {
                audioSource.Play();
            }
        }
        else
        {
            audioSource.Stop();
        }
    }

    private void Rotate()
    {
        rigidBody.freezeRotation = true; // take manual control of rotation
       
        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; // resume physics control of rotation
    }
}

The code is working as expected but the states just doest show up in the inspector.

Hi Andreas,

I’m not sure if you are referring to the enum, or the variable state which holds the currently state, however, in both cases in the code example, there is no access modifier specified, as such, C# will default to private.

Rigidbody rigidBody;
AudioSource audioSource;

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

All of the above will be treated as private

I should add, the use of [SerializeField] will expose the variables in the Inspector regardless of their access modifier, as per these;

[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;

Hope this helps. :slight_smile:


See also;

Yep, need to add public or [SerializeField] to your state variable.

This just seems really weird cuz if you follow the lecture, Ben does none of that.
note I’m using an exact copy of his script and it doesn’t show up in the inspector like it does for him.
so any code changes shouldn’t be necessary

His shows like that:

Am actually not to that lecture yet so will keep an eye out for it.

Afternoon Andreas,

there is “Normal” and “Debug” modes for the inspector.​
image
If you have a look at the top of the inspector window it is labelled “Debug”. as mentioned in a previous lecture this allows the private member variables to be visible, but not editable.

going to the three dashed lines symbol in the top right, click and select Debug (second one down) and you should now see these but greyed out and uneditable.

should be good to go :slight_smile:

Ive popped a reply in the QnA as well :wink:

5 Likes

Thanks a lot <3

2 Likes

I feel like an idiot now x)

not at all !!
If it werent for questions none of us would learn.
Its also one of those little Unity obsucuries that you seldom find yourself using, and now that you noticed it and went playing with the options again, its something that will stick in your Unity toolbag memory.

so win win all round :slight_smile:

always question things that are not right or unclear or things that your looking for chat about, theres loads of people on this community that are more than willing to help out, as were all in the same learning boat…

3 Likes

Privacy & Terms