Search Unity

[Resolved] Protecting custom Components like Transform etc

Discussion in 'Scripting' started by Cyber-Dog, Dec 21, 2019.

  1. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    168
    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...
    Code (CSharp):
    1. public class Transform : Component, IEnumerable
    2. {
    3.     protected Transform();
    4. ...
    I tried doing that on my own component but I got an error...
    Code (CSharp):
    1. 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 :D
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    168
    I worked it out through a bit of tinkering :)

    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.
     
unityunity