Making a rotating object rotate with a parent object

I have a game object which swings back and forth along one of its axis, this is attached to a parent gameObject which moves around a scene. However, when the parent object rotates the child object does not rotate with it.

I expect this is something to do with local vs global space… however Im at a loss…

This is the code which manages the sweep.

    // Increment a timer
	timer += Time.deltaTime;

	// Swing the object back and forth based on the rotationLimit and the rotationSpeed
	currentRotation = Mathf.PingPong (timer * rotationSpeed, rotationLimit * 2f) - rotationLimit;

	// Create a temporary variable to store the new rotation
	Quaternion rotation = Quaternion.Euler (transform.rotation.x, transform.rotation.y + currentRotation, transform.rotation.z);

	// Set the rotation to the temp var
	transform.rotation = rotation;

I considered grabbing the parents rotation and using that instead of the transform.rotation. However, ideally this same script needs to work on a variety of objects, some of which don’t have parents. Can anyone advise where I am going wrong.

Try using transform.localRotation instead of transform.rotation.

transform.localRotation = rotation;