JediMan
JediMan

Reputation: 43

How to initialize transform without creating game object in inspector?

I want to initialize transform without creating game object, but something like 'Transform trans = new Transform();' isn't working due to protecting level. I have an error in my code

Transform trans;
trans.position = new Vector3(0f, 0f, 0f);

because trans equals null. So, why it isn't working?)

Upvotes: 0

Views: 5779

Answers (3)

thunderkill
thunderkill

Reputation: 146

if you need a transform then you can create one don't use the defined transform by Unity , but to answer your question properly you can't create a transform without a gameobject the concepts . A component (transform , text , audio ...) all of these are gameobject on their own as well you can Instantiate a tranform class by saying Transform transform ;

Upvotes: 0

Ruud van Gaal
Ruud van Gaal

Reputation: 149

This was annoying indeed. For my case it was enough to create my own class (without Scale since I didn't need it):

class MyTransform
{
  public Vector3 localPosition;
  public Quaternion localRotation;

  public MyTransform(Transform tf)
  {
    localPosition = tf.localPosition;
    localRotation = tf.localRotation;
  }
}

Then create one:

MyTransform tf = new MyTransform(go.transform);

and use it for the functionality it exposes (not much here):

tf2.localRotation = tf.localRotation * go.transform.localRotation;
tf2.localPosition = tf.localRotation * go.transform.localPosition + tf.localPosition;

Upvotes: 0

Zac Guadagno
Zac Guadagno

Reputation: 76

You cannot create a Transform without a GameObject.

Instead, you could create two Vector3 (one used for position and one used for scale) along with a Quaternion to handle rotation.

The combination of the methods within these classes could be used to handle what you are seeking to use a Transform for.

Upvotes: 4

Related Questions