[Resolved] Protecting custom Components like Transform etc

Hey,

I’m creating a custom component for a unity asset. Instantiating public instances of it without being added as a component will break functionality.

I know Unity already helps by adding a warning when a MonoBahaviour is instantiated that way.
But I would like to completely block it the way a Transform does.

For instance, if you go.
Transform transform = new Transform();
You will get an error…
Transform.Transform()' is inaccessible due to its protection level

I looked into the code of the Transform component, and what seems to do it as a constructor…

public class Transform : Component, IEnumerable
{
    protected Transform();
...

I tried doing that on my own component but I got an error…

SelectableObjectRTS.SelectableObjectRTS()' must declare a body because it is not marked abstract, extern, or partial

However the Transform does not seem to, plus things like abstract would break in unity.

Any help will be appreciated :smile:

I worked it out through a bit of tinkering :slight_smile:

I needed to add double brackets instead.
protected SelectableObjectRTS() { }

Im guessing as the Transform code is hidden by a DLL, that must be how it shows.