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

[SystemSerializable] not working

Discussion in 'Scripting' started by AbradolfLinkler, Apr 19, 2017.

  1. AbradolfLinkler

    AbradolfLinkler

    Joined:
    Feb 26, 2017
    Posts:
    26
    Hi Guys,

    Trying to follow the tutorial, I'm trying to make the Granice class fields visible in the inspector, but neither the class nor the floats show up.

    What is wrong with this code?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6.  
    7. [System.Serializable]
    8. public class Granice : System.Object {
    9.  
    10.     public float xmin, xmax, ymin, ymax;
    11.  
    12. }
    13.  
    14. public class PlayerControl : MonoBehaviour {
    15.  
    16.     Rigidbody rb;
    17.     public float playerSpeed;
    18.     GameObject lufa;
    19.     Granice granice;
    20.     Vector3 pozycjaStatku;
    21.  
    22.     void Start () {
    23.         granice = new Granice ();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28. ruszajStatek();
    29.     }
    30.  
    31.     void FixedUpdate(){
    32.      
    33.     }
    34.  
    35.     void ruszajStatek ()
    36.     {
    37.         float horizMov = Input.GetAxis ("Horizontal");
    38.         float vertMov = Input.GetAxis ("Vertical");
    39.  
    40.         pozycjaStatku.x = Mathf.Clamp (transform.position.x, granice.xmin, granice.xmax);
    41.         pozycjaStatku.z = Mathf.Clamp (transform.position.z, granice.ymin, granice.ymax);
    42.  
    43.         rb.velocity = new Vector3 (horizMov, 0f, vertMov) * playerSpeed;
    44.  
    45.         transform.position = pozycjaStatku;
    46.  
    47.         transform.rotation = Quaternion.AngleAxis (45, new Vector3 (0, 0, horizMov));
    48.  
    49.  
    50.     }
    51. }
    52.  
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    "Granice" needs to derive from Monobehavior.... All classes derive from System.Object. You should not need this explicitly.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    It does not need to derive from MonoBehaviour, OP's trying to create a data container.

    @Electrino123456, the same rules hold for [Serializable] classes as normal fields; if you want them to show up in the inspector, they either have to be public, or be marked with [SerializeField]

    so:

    Code (csharp):
    1. public Granice granice;
    2. //or
    3. [SerializeField]
    4. Granice granice;
     
    DezBoyle, AShenawy and DanielQuick like this.