The inspector does not display my C# variables

Hi

I have the following code and while the inspector shows the variable v it never displays the variable a.

What am I doing wrong?
thanks

using UnityEngine;
using System.Collections;

public class LevelObjects : MonoBehaviour 
{
		
	public int v=0;
	public ClicObject a;	
	// Use this for initialization
	void Start () 
	{
				
	
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}
public class ClicObject
{		
	public string name;
}

Welcome to the forums! :smile:

By default custom C# classes like your ClicObject aren’t serializable, thus they don’t appear in the Inspector. If you want to change that then you have to add a line to explicitly do that:

[System.Serializable]
public class ClicObject 
{
  public string name;
  
}

Done!

Tip: when you post code here on the forums, either use the Code button up top or manually wrap it in a code block ([ code ]…[ /code ], without the spaces). I’m going to edit your post so you can see an example. :slight_smile:

Great it worked !

Thanks

If we define a class within a C# script (which must in itself be a class), how can we get the class variable to show up in the Inspector?

In Javascript, I could do it like:

class CoolClass {var coolVariable : int;}
var coolClass : Class;

and then coolVariable could be set in the Inspector. How can I do this kind of thing with C#?

by doing the same class mess as in JS with inner classes

below code is nothing else than

class scriptName {
  var coolClass: CoolClass
  class CoolClass {
    ...
  }
}

Here is corresponding dummy code (working)

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

  public CoolClass coolClass;
	[System.Serializable]
  public class CoolClass {
		public int test;
  }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

I had tried something very similar to that, but it didn’t work. When I did it exactly like you did, it did work. Maybe I made a capitalization error the first time?

Whatever the case, thanks.

So there is another solution which is to make the variable public , you will find them showing up in the inspector :

public class VariableTest : MonoBehaviour {
	
	public int health ;
	public float myspeed = 10.0f;
	public string someString = "this is a test";
	public string some_name = "fred";
	public bool someSeeting = true;
	public GameObject someObject ;
	
	// Use this for initialization
	void Start () 
	{...

So you necro a 4 years old thread and you didn’t even read the comments :stuck_out_tongue: