Using Enums in Behaviours

Hey,

I somehow can’t use an Enum in my Behaviour, although I’ve seen you guys making use of it in other components. I probably miss something obvious here?

export enum Zones {
  START = 0,
  NEWS = 1,
  SOLUTIONS = 2,
  MACHINE = 3,
}

// Keep list of all zones and provide finder
export class ZoneController extends Behaviour {
  @serializable(Object3D)
  public avatar:GameObject|null = null;

  @serializable(Object3D)
  public vrCamera:GameObject|null = null;

  @serializable()
  public zone: Zones = Zones.NEWS;

Results in:

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

#pragma warning disable

namespace Needle.Typescript.GeneratedComponents
{
   public partial class ZoneController : UnityEngine.MonoBehaviour
   {
       public UnityEngine.GameObject @avatar;
       public UnityEngine.GameObject @vrCamera;
       // public Zones @zone; → Could not resolve C# type
   }
}

// NEEDLE_CODEGEN_END

Any ideas on that?

Original Post on Discord

by user 827095900392259645

You have to define the enum type manually in C# (e.g. outside of the NEEDLE_CODEGEN guards)

Because codegen does only generate types for components and not any other type at the moment

Alright, thank you!

by user 827095900392259645

@marwie1 :cactus: Sorry to tag you here - but seeing as youve answered before, could you please elaborate on this? Im facing the same issue and dont follow the response above.

by user 259070246382469121

If you created the enum in your typescript code you need to also create it in C# (if it didnt exist there yet).
Meaning: add for example a new C# script somewhere in your project and add the C# enum there. You might need to restart Unity once so the component generator is aware of the new type

Ah thank you, it was the restart that was needed.

by user 259070246382469121