how to rotate a gameobject in unity 2d code example

Example 1: how to make a object rotate towards a position in 2d unity

Vector3 targ = staticCompassTarget.transform.position;
 targ.z = 0f; 
 Vector3 objectPos = transform.position;  
 targ.x = targ.x - objectPos.x;  
 targ.y = targ.y - objectPos.y;      
 float angle = Mathf.Atan2(targ.y, targ.x) * Mathf.Rad2Deg;   
 transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

Example 2: rotation unity script 2d

transform.eulerAngles = Vector3.forward * degrees;
// or
transform.rotation = Quaternion.Euler(Vector3.forward * degrees);
// or
transform.rotation = Quaternion.LookRotation(Vector3.forward, yAxisDirection);
// or
transform.LookAt(Vector3.forward, yAxisDirection);
// or
transform.right = xAxisDirection;
// or
transform.up = yAxisDirection;

Example 3: rotation unity script 2d

transform.Rotate(Vector3(0, 0, 50));
//instead of :
transform.eulerAngles = new Vector3 (0, 0, 50);
//or like you said
transform.eulerAngles = Vector3.forward * 50;