copying rotation

I 'm using a Quaternion.Slerp to rotate an object, and I need a way to set its X any Y rotations to be equal to its parent object. The Z rotation is the only one I’m trying to change. My code looks like this;

var smooth = 2.0;
var angle = Quaternion.identity;
var tiltAngle;

function Update(){
angle.eulerAngles = Vector3(0, 0, tiltAngle);

transform.rotation = Quaternion.Slerp(transform.rotation, angle, Time.deltaTime * smooth);
}

The variable tiltAngle is a float that I’m using to control the Z rotation, it’s not important.

Its the two zeros in the line “angle.eulerAngles = Vector3(0, 0, tiltAngle);” that I need to replace to the parent’s rotation. How can I do this?

replace the zero with parent.transform.eularAngles.x, and same for y.

I replaced it with transform.parent.transform.eularAngles.x and I got this error.

NullReferenceException: Object reference not set to an instance of an object

This object is definitely the child of another object.

OK, parent is a transform, so it should be:
transform.parent.eularAngles.x

transform.parent.eularAngles.x gets the same error.

I don’t know what to tell you because I tried it on a child object and it printed the angle.
try:
if(transform.parent != null) print(“has parent”);

If the transform that you’re rotating is a child of the transform that you’re trying to copy, then use transform.localRotation of transform.rotation.