C# Variables not showing up in inspector

I’m making my game using C# scripts, and the variables aren’t appearing in the inspector, I could just go into the code to edit variables, but having them in the inspector would be far more convenient.

I’ve tried every solution I found online, I’ve tried making the variables public, didn’t work, I corrected any errors in my code, still not appearing, I tried System.Serializable, nothing.

So what do I do now?

Here’s the code I wrote to test it:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class SomeMouseLookScript : MonoBehaviour {
	
	public enum rotationAxis {mouseX = 0, mouseY = 1}
	public int mouseXY;
	
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}

I wrote this while following a tutorial, neither variables appeared in the inspector.

I’m not sure if this actually is your problem, but is assume the int shows up but you also want to see the enum. If that is the case, well, an enum is not a variable. It rather defines a new type of variable. Like if you declare a variable as integer means, that this variable can have values like {…, -1 , 0 , 1 ,…}. Tu use the enum you have to declare a new variable as your enum:

public enum rotationAxis {mouseX , mouseY }
public rotationAxis someFancyName;

Then the someFancyName variable will show up in the inspector and you can choose it to be either mouseX or mouseY. To use the variable inside of your script you will have to use rotationAxis.mouseX to check for it. Like:

if(someFancyName == rotationAxis.mouseX){

//do something

} else {

//do something else

}

The variables have to be public and part of the class, but not part of any method. Make sure the script is compiling as any changes won’t show if the new script hasn’t compiled.

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {
	
	public int number = 10;
	public string word = "Hello World";

	void MyMethod()
	{
        int anotherNumber = 10;
		
		string anotherWord = "Hello World";
		
	}
}

So here the only ones that will show in the inspector and number and word. You could also try re-adding the script, but I don’t think that will be your problem.

If you just edited the script, saved/recompiled, try using Assets>Refresh (Ctrl+R)

It could also be a noobie issue. (It was for me). I did not notice I had a build error. Check your console.

If anyone is having trouble getting a variable of custom type to display in the editor, be sure that said custom type derives from MonoBehaviour. That was my problem.

I know this question’s old, but another answer that I found solved my run-in with this problem:

If you’ve refactored or renamed your script after adding it as a component to an object, it will stay as a supposedly valid script and will compile etc, but will only show the variables in the Inspector if your script’s name and the class name are the same (so no ExampleScriptHere and Example_Script_Here nonsense).

I had the same issue. It has happened a few times and it is usually just a case of removing the script from the gameobject and re-adding it (I guess it is a unity compiling bug?).

However, a few times it has happened where simply removing then re-adding the script to a gameobject does not fix it. For these times, I had to delete the script itself, then remove the script from the gameobjects, then re-make the script and re-add it to the gameobjects. (IF YOU EVER HAVE TO DO THIS PLEASE MAKE SURE YOU HAVE A BACKUP OF THE CODE WITHIN THE SCRIPT BEFORE YOU DELETE IT).

@Cobradabest : not directly applied to your example, but maybe this will help someone else who, like me, is a noob trying to figure this out.

Static objects don’t appear in the inspector.
public static GameObject canYouSee; // does NOT appear in Inspector
public GameObject canYouSee; // DOES appear in Inspector

FYI: Maybe don’t use statics anyway. Try this: https://forum.unity.com/threads/singleton-vs-static.197169/