Rotate RectTransform/UIImage per script?

Hi there,

i wanted to build an old fashioned watch (with moving hands) in my GUI. I put the background image on a canvas and then also two UIImages for the two hands. I positioned them and pivoted them to the center of the background image (the actual clock image) and my plan was to rotate these images according to the time. Unfortunately there seems no way to access the rotation of the rectTransform from the script? I tried to access the rotation directly on the rectTransform, I tried to get a transform and I searched a while but found no solution.
I don’t want to make 60 single images and swap them every minute and I don’t want to do an animation and control that by code (if even possible).
So please can you give me some advice on this?

Thanks a bunch!

edit: since RectTransform inherits from Transform, why can’t I access the transform.raotation…?

Gnaa silly silly me…after working too long on one thing you don’t see the simplest mistake…I tried to set the rotation OUTSIDE of a method…:roll_eyes:
So bottom line: RectTransform.rotation works just fine…

1 Like

I need to do this, But Im not sure how you solve it, can you please give me more detail on the c sharp code of how to do it?
Thanks!

Of course, here is my script for the clock.
My GameManager script has s static variable named clock which is increased in every update. Of course you could maks a clock variable inside the clock script.
In the inspector you have to drag a text field and the two UIImages for the both hands in the public fields of the script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Clock : MonoBehaviour {

    public Text display;
    public RectTransform littleHand;
    public RectTransform bigHand;
   
    Transform test;
   
    int hours;
    int minutes;
    string paddingChar="0";
    // Use this for initialization
    void Start () {
    }
   
    // Update is called once per frame
    void Update () {
      hours=Mathf.RoundToInt(GameManager.clock/60f);
      minutes=Mathf.RoundToInt(GameManager.clock%60);
      if (minutes<10){
          paddingChar="0";
      } else {
          paddingChar="";
      }
          display.text=hours.ToString ()+":"+paddingChar+minutes.ToString();
          littleHand.rotation=Quaternion.Euler(0,0,-hours*360/12);
          bigHand.rotation=Quaternion.Euler(0,0,-minutes*360/60);
    }
}
1 Like

What is the purpose of line 11. Transform test; ?

Oh sorry, that has no purpose. It was left over from one of my previous tests :slight_smile:

you’re so cute