Whats the correct way to create a serializable class that can have values assigned in the inspector?

I have been referencing the documentation here in order to create a custom class that allows me to assign the class values in the inspector. Script Examples | Needle Engine Documentation

I have a Racer.cs class, marked as serializable (shown in image). I then have the equivalent Racer class in typescript, but when I create a variable of that type in a script, it doesnt allow me set the fields in the inspector as I would expect, instead it wants an instance of the Racer class rather than allowing me to create an instance in the inspector and assign values.

Image 1 - The Racer.cs script
Image 2 - The typescript equivalent and usage in another script
Image 3 - The result in my Needle project
Image 4 - The desired result (taken from my separate, working standard Unity project)
image.png
image.png
image.png
image.png

Original Post on Discord

by user 259070246382469121

Which type is being used in the RacerList in C# by codegen?
And you can also use the classes from your standard Unity project if you already have them - you don’t need to use the component compiler in this case (just mark your typescript classes accordingly so it doesnt generate C# scripts for them)

Looks to me like you have multiple classes named β€œRacer” in your project and the one that is chosen here is not marked with [Serializable] but something else / unexpected

If you want to go half-half and generate your class but manage this specific field yourself manually (because a wrong type is chosen for example and you dont want/can not use your existing component) then you can also mark the field in typescript with @nonSerializable (please see in the docs) and add the explicit field yourself in the C# partial class (outside of the codegen boundary comments or in a new file)

I get what youre saying, but I dont understand what is going on here with the codgen. First and foremost, the issue is resolved by renaming my class to RacerEntry instead of Racer, so thats great.

However, can you please explain the following.

Here is my Racer class (Ive had to rename it to RacerEntry from Racer) and I have updated my variables to be a Transform, Sprite, and int.
[Serializable] public class RacerEntry { public Transform racerTransform; public Sprite racerSprite; public int racerNumber; }

Here is the typescript usage for Racer

class RacerEntry { public racerTransform : GameObject | undefined; public racerSprite : Sprite | undefined; public racerNumber : number = 0; }

And here is the RacerEntry class being used in another script

@serializable(RacerEntry) public racers : RacerEntry[] = [];

Here is the the codegen for Racer.cs (Notice how this is not updated to have the Transfom, Sprite, and int), yet in the Assets/Needle/Components.codegen that is the only Racer entry

`// NEEDLE_CODEGEN_START
// auto generated code - do not edit directly

#pragma warning disable

namespace Needle.Typescript.GeneratedComponents
{
public partial class Racer : UnityEngine.MonoBehaviour
{
public string @racerName = β€œβ€;
public int @racerNumber = 0;
}
}

// NEEDLE_CODEGEN_END
`

So while it works, I dont really understand why. I assume that the Assets/Needle/Components.codegen files would update every time a change is made to the corresponding C# files. I have searched my workspace and no other class called Racer exist, bar the C# I have shown, and the codegen equivalent.

by user 259070246382469121

  • C# components are re-generated when a change is detected in a Typescript (or js) file
  • if you rename the class in typescript you will get a different C# file with the new type (that is as long as you put your scripts into the src/scripts directory)