Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Fields do not shown up in inspector (class inherits from a Unity Script)

Discussion in 'Scripting' started by Sbizz, Jun 10, 2015.

  1. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Hey!

    I tried to do a script that extends some functions of an existing script but I'm currently blocked on something stupid and I have no idea why it does that.

    The script can not be more simple :

    Code (CSharp):
    1. public class UIContentSizeFitter : ContentSizeFitter {
    2.     public float m_MaxHeight;
    3.     public float m_MaxWidth;
    4. }
    Yes. Only that. :p I want these variables to be accessible from the inspector and I have no idea why they just don't shown up..

    Any idea ?

    Thank you :)
     
  2. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    The parent class needs/must inherit from MonoBehaviour

    Parent Class

    Code (CSHARP):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6. [Header("Variables From Parent Class")]
    7. public string Text;
    8. public int Int;
    9. }


    Child Class
    Code (CSHARP):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class TestmyTest : Test
    4. {
    5. [Header("Varibale From Child Class")]
    6. public string TestMyTest;
    7. }


    Result Using Unity5:
     
    Last edited: Jun 10, 2015
  3. Deleted User

    Deleted User

    Guest

    You need to inherit from MonoBehaviour.
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You can also inherit from UIBehaviour, your variables are going to be shown inside the Inspector.

    ContentSizeFitter inherits from UIBehaviour..

    Code (CSharp):
    1. public class UIContentSizeFitter : UIBehaviour {
    2.     public float m_MaxHeight;
    3.     public float m_MaxWidth;
    4. }
    This works.
     
    5vStudios likes this.
  5. Deleted User

    Deleted User

    Guest

    UIBehaviour inherits from MonoBehaviour. Are you saying you're still unable to see your public variables in the inspector?
     
  6. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Okay, found the issue..

    There is a ContentSizeFitterEditor and it doesn't call DrawDefaultInspector. So it only shows what the script ContentSizeFitterEditor wants..

    So I created my own script too..

    Code (CSharp):
    1. [CustomEditor(typeof(UIContentSizeFitter), true)]
    2. public class UIContentSizeFitterEditor : ContentSizeFitterEditor {
    3.     public override void OnInspectorGUI() {
    4.         DrawDefaultInspector();
    5.     }
    6. }
    7.  
     
    mhardy, Yakirbu and yelaex like this.