Home > Software design >  Public variables created in Rider do not show up in Unity
Public variables created in Rider do not show up in Unity

Time:01-29

I just started using Unity (which came with VSC), but I had better experience using JetBarin products such as IntelliJ IDEA, so I went and switched to Rider. However, I am now unable to connect public variables (int, float, GameObject) to my Unity projects.

I tried updating Rider and changing some setting, but came none the wiser.

UPDATE: There have been (obvious) request for my code to see the exact issue, so I hope this helps clear up the issue a little bit:

Code written in VSC

The resulting public variables showing up in Unity

Similar code written using Rider

No interactive variables showing up in Unity

CodePudding user response:

Unity serializes only fields (not properties with get or set) of MonoBehaviours. All public fields are serialized unless have [System.NonSerialized] attribute.

DO NOT get confused with the [HideInInspector] attribute, it won't be visible in the inspector (if you don't have a custom inspector) but WILL BE serialized.

class Foo
{
    // Bar won't be shown in the inspector or serialized.
    [System.NonSerialized]
    public int Bar = 5;
}

To serialize a non-public field use [SerializeField] attribute for primitive types (such as int, float, bool).

public class SomePerson : MonoBehaviour
{
    // This field gets serialized because it is public.
    public string name = "John";

    // This field does not get serialized because it is private.
    private int age = 40;

    // This field gets serialized even though it is private
    // because it has the SerializeField attribute applied.
    [SerializeField]
    private bool isMale = true;
}

If you wanna serialize own class or struct, use [System.Serializable] attribute.

[System.Serializable]
public struct PlayerStats
{
    public int level;
    public int health;
}
  • Related