- Home /
Component without gameObject
Hi,
I wonder if it would be possible to create a component, say a Transform, without a gameObject.
Something like
public Transform _transform = new Transform () ;
So that it would only exists within the MonoBehaviour that declares it When I try, I get the following error message : UnityEngine.Transform.Transform()' is inaccessible due to its protection level
Documentation doesn't mention a constructor though.. I guess it's not possible.
Answer by robertbu · May 12, 2013 at 12:21 AM
It is impossible to create a transform without a game object. I it impossible to create a game object without a transform.
http://docs.unity3d.com/Documentation/Manual/TheGameObject-ComponentRelationship.html
If you need to dynamically create a transform, you can do something like:
public Transform _transform = new GameObject().transform;
You can also define your own classes not derived from Monobehaviour. You deal with these classes like you would classes outside of Unity (i.e use 'new' to create and use a constructor (not Start() to initialize).
Answer by EION Technology · May 12, 2013 at 12:18 AM
Usually the protection level means that it needs to be static. Try:
public static Transform _transform = new Transform () ;