0
\$\begingroup\$

I am trying to get a clock tower to rotate it's analog time according to the actual time.

However, the mesh or materials won't rotate using the "DateTime.Now.TimeOfDay; hours.localRotation = Quaternion.Euler" method. It stays still. Can anyone please help?

Code:

private const float
    hoursToDegrees = 360f / 12f,
    minutesToDegrees = 360f / 60f,
    secondsToDegrees = 360f / 60f;

public Transform hours, minutes, seconds;
public bool analog;

void Update()
{
    if (analog)
    {
        TimeSpan timespan = DateTime.Now.TimeOfDay;
        hours.localRotation =
            Quaternion.Euler(0f, 0f, (float)timespan.TotalHours * -hoursToDegrees);
        minutes.localRotation =
            Quaternion.Euler(0f, 0f, (float)timespan.TotalMinutes * -minutesToDegrees);
        seconds.localRotation =
            Quaternion.Euler(0f, 0f, (float)timespan.TotalSeconds * -secondsToDegrees);
        print(timespan);
    }
    else
    {
        DateTime time = DateTime.Now;
        hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);
        minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);
        seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);
    }
}
}

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ I see you have an Unassigned Reference Exception being reported in your console. Did you forget to assign a reference used by your script, causing it to abort execution? I also see you have an Animator on this object. Is an animation potentially overriding your script's orientation changes? I also also see your screenshot has the transform gizmos in "global, center" mode. Can you retake this image in "local, pivot" mode so we can understand the origin and orientation of these objects from their gizmos? \$\endgroup\$
    – DMGregory
    Commented Dec 7, 2022 at 14:32

1 Answer 1

0
\$\begingroup\$

A good debugging trick: keep your Console tab visible, and read all the errors it prints.

A correctly coded game will print no errors here. If you have errors, the compiler or runtime are telling you that you made a mistake, and you should ensure all such mistakes are fixed before looking for problems elsewhere.

If you have trouble understanding an error message, put the exact text into a search engine. You won't be the first person to see this message, so you'll quickly find existing Q&A and guides that tell you what to do about it.

In this case, the runtime is telling you that you have an "UnassignedReferenceException: The variable seconds of BigBen has not been assigned."

This means that you forgot to tell BigBen which object it should be rotating, by dragging that object into the corresponding field in the script's inspector, or using an appropriate Find... method. When it tries to rotate this object that it hasn't been given, the script aborts, and no code beyond that point will execute this frame.

There may be other problems once you fix this one. Ensure you fix every error message before posting here, to eliminate the easy stuff and isolate the problem you really need help with.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .