Transform constructor in c#

I feel like I must be missing something here.
I’m writing a function in which I need to parent a dummy transform to an object temporarily so I can get the information relative to the object. ex: what is the point 3 units in front of the player.
I’m trying to construct a new transform and don’t seem to be able to. The code I have is:

Transform tempTrans= new Transform();
tempTrans.parent = transform;
tempTrans.localPosition = new Vector3(0,0,3);

Unfortunately this throws the error:

error CS0122: 'UnityEngine.Transform.Transform()' is inaccessible due to its protection level

I’m sure I’m doing something horribly wrong, like… I should be constructing objects in C# differently than new Object()? I really don’t know, so any help is greatly appriciated.

1 Like

Transform is a component. You can’t create components like that, since they don’t exist by themselves; you can create a GameObject, and add components to it.

You don’t need to do any of that, though; use TransformDirection.

–Eric

1 Like

Thanks for the quick reply. Then it sounds like I’m going to have to do the math myself, since I don’t believe there is a rotateVector function.
Thanks,
–Tristfall

I don’t think you read my second paragraph.

var pos = transform.TransformDirection(Vector3.forward * 3);
// or
pos = transform.forward * 3;

–Eric

Oh, Yeah, apparently I’m blind.
Thanks, that works perfectly.

But now what do I do with all this vector math I just commented out?

Thanks a lot,
Trist

Apparently there’s a rather profitable black market for lightly used vector math in Kyrgyzstan, but you didn’t hear that from me.

–Eric

8 Likes