Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Serialized Field component not showing in inspector

Discussion in 'Scripting' started by orangedu02, May 30, 2019.

  1. orangedu02

    orangedu02

    Joined:
    Apr 26, 2019
    Posts:
    4
    As the title says, I'll post my script just in case, it shows no errors and I'm a noob so I can't for the life of me figure this out even after doing research. My goal is to make doors open and close displaying the appropriate animation for each state.
    Thanks in advance to anyone responding to this.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Door : MonoBehaviour
    6. {
    7.     Animator anim;
    8.  
    9.     public GameObject doorType;
    10.  
    11.     int stateOfDoor = 1;
    12.  
    13.     void Start()
    14.     {
    15.         anim = GetComponent<Animator>();
    16.  
    17.         if (doorType.name == "EntryDoor")
    18.             OpenDoor();
    19.  
    20.         if (doorType.name == "ExitDoor")
    21.             LockDoor();
    22.     }
    23.  
    24.     void LockDoor()
    25.     {
    26.         if (doorType.name == "ExitDoor")
    27.         {
    28.             anim.SetFloat("DoorState", 1);
    29.             stateOfDoor = 1;
    30.         }
    31.     }
    32.  
    33.     void UnlockDoor()
    34.     {
    35.         if (doorType.name == "ExitDoor")
    36.         {
    37.             anim.SetFloat("DoorState", 2);
    38.             stateOfDoor = 2;
    39.         }
    40.     }
    41.  
    42.     void OpenDoor()
    43.     {
    44.         if (doorType.name == "ExitDoor")
    45.         {
    46.             anim.SetFloat("DoorState", 3);
    47.             stateOfDoor = 3;
    48.         }
    49.     }
    50.  
    51.     public void SetDoorState(int state)
    52.     {
    53.         if (state == 1 && doorType.name == "ExitDoor")
    54.             LockDoor();
    55.         if (state == 2 && doorType.name == "ExitDoor")
    56.             UnlockDoor();
    57.         if (state == 3 && doorType.name == "ExitDoor")
    58.             OpenDoor();
    59.     }
    60.  
    61.     public int GetDoorState()
    62.     {
    63.         return stateOfDoor;
    64.     }
    65. }
    66.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    There aren't any serialized fields in this script.
    You need to decorate a non-public field with the
    [SerializeField]
    annotation in order to serialize it and have it show in the inspector.

    Example:
    Code (CSharp):
    1. public class Example : MonoBehaviour {
    2.    [SerializeField]
    3.    private int myInt;
    4. }
     
    TaleOf4Gamers likes this.
  3. orangedu02

    orangedu02

    Joined:
    Apr 26, 2019
    Posts:
    4
    I tried that and it didn't work
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Were you getting any errors in the console?
     
  5. orangedu02

    orangedu02

    Joined:
    Apr 26, 2019
    Posts:
    4
    There were a bunch of errors, but I fixed it now, I deleted the script and the game object I was trying to hoot the script to , reimported all my assets then redid the script from scratch in a new file and then re added the object to my scene and it all worked out.
    Sorry for the inconvenience and ty for your replies