My variables are not showing in the inspector
Hi everyone, I have been following a tutorial online but unfortunately, some of the variables he has are not showing in my inspector. Public class buildObjects variables name, prefab, preview and cost.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Building : MonoBehaviour {
public List<buildObjects> objects = new List<buildObjects>();
public buildObjects currentObject;
private Vector3 currentPos;
public Transform currentPreview;
public Transform cam;
public RaycastHit hit;
public LayerMask layers;
public float offset = 1.0f;
public float gridSize = 1.0f;
public bool IsBuilding;
public void ChangeCurrentBuilding()
{
GameObject curprev = Instantiate(currentObject.preview, currentPos , Quaternion.identity) as GameObject;
currentPreview = curprev.transform;
}
public void startPreview()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, 10, layers))
{
if (hit.transform != this.transform)
{
showPreview(hit);
}
}
}
// Use this for initialization
void Start () {
currentObject = objects [0];
ChangeCurrentBuilding();
}
// Update is called once per frame
void Update () {
if(IsBuilding)
{
startPreview ();
}
if(Input.GetButton("Fire1"))
{
Build();
}
}
public void showPreview(RaycastHit hit2)
{
currentPos = hit2.point;
currentPos -= Vector3.one * offset;
currentPos /= gridSize;
currentPos = new Vector3(Mathf.Round(currentPos.x), Mathf.Round(currentPos.y), Mathf.Round(currentPos.z));
currentPos *= gridSize;
currentPos += Vector3.one * offset;
currentPreview.position = currentPos;
}
public void Build ()
{
previewObject PO = currentPreview.GetComponent<previewObject>();
if(PO.isBuildable)
{
Instantiate (currentObject.prefab, currentPos, Quaternion.identity);
}
}
public class buildObjects
{
public string name;
public GameObject prefab;
public GameObject preview;
public int cost;
}
}
Answer by haruna9x · Aug 06, 2017 at 10:19 AM
For classes not derived from MonoBehaviuor, you need to add [System.Serializable] so that Unity can serialize it.
[System.Serializable]
public class buildObjects
{
public string name;
public GameObject prefab;
public GameObject preview;
public int cost;
}
Thank you so much! I have been ripping my hair out. Absolute champion!
Your answer
Follow this Question
Related Questions
public bool not showing in inspector 1 Answer
How to save a two-dimensional array, as part of the variable inspector? 0 Answers
Serialize a list of class containing ScriptableObject assets 0 Answers
[SOLVED] ScriptableObject generic list makes all derived instances to base type when serializing 1 Answer
Change in Editor via Script Values of another Script 2 Answers