Home > Net >  Unity: Make public static Object assignable in Inspector
Unity: Make public static Object assignable in Inspector

Time:01-03

So, I have a Cinemachine Virtual Camera, that I need to assign in the Inspector but the problem is it's a public static one, so it's not shown in the Inspector. I tried to set the Inspector to Debug, but it's still not showing up. I need to have it public and static because I need to access it from another script.

As you probably know, it didn't work making it only public, because then it's not accessible in the other script. I've tried with and without [SerializeField], still the same result.

Are there other methodes to make it accessible throught other scripts and how can I solve my Problem?

CodePudding user response:

You could try using FindObjectOfType() if you only have one camera in your level in order to reference it on awake and make it easier for you like

public static CinemachineVirtualCamera Cam;

private void Awake()
{
    Cam = FindObjectOfType(CinemachineVirtualCamera);
}

If you REALLY want to make static fields available to change in the inspector you might want to look into singletons and custom inspectors but I am not familiar enough with those to give you advice.

CodePudding user response:

if you make your class a singleton (i.e. do instance=this in awake), you can then do:

public static myField {get { return instance._myfield;}}
  • Related