- Home /
Problem with Simple Script - Rotation and Data Types
Basically, this was supposed to be a small script for the sun of one of my projects. It simply sets the location of the sun in the sky with the location it is supposed to represent, and then proceeds to cycle the sun along the X axis at the rate determined by the 'hoursInDay' variable'
fortunately, the first part of the functionality works perfectly, though the second part has a ridiculous problem which I feel so stupid for not being able to solve. the 'rot' variable will not go to anything other than zero. I can manually enter the rotation speed in the Rotate function, but cannot use the variable no matter what I try... casting the original variable, ect. Would anyone be kind enough to nudge a newbie in the right direction?
#pragma strict
var hoursInDay = 1;
private var rot = 15 / (hoursInDay * 60 * 60 / 24);
function Start () {
//Setting the sun to the right position
var time = (System.DateTime.Now.Hour - 6);
transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
}
function Update () {
transform.Rotate(rot * Time.deltaTime, 0, 0);
}
Answer by Matthew0123 · Jan 26, 2013 at 04:25 AM
#pragma strict
var hoursInDay = 1;
private var rot = 15 / (hoursInDay * 60 * 60 / 24);
function Start () {
//Setting the sun to the right position
var time = (System.DateTime.Now.Hour - 6);
transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
}
function Update () {
transform.Rotate(rot * Time.deltaTime, 0, 0);
}
There is an a very common error here. I will state one of the most common rookie error is you CANNOT assign a VAR a value OUTSIDE of a function. So your vars are not set to a value and they're undefined. Do this to correct it.
#pragma strict
var hoursInDay: float;
private var rot: float;
function Start () {
//Assigning Variables
hoursInDay = 1;
rot = 15 / (hoursInDay * 60 * 60 / 24);
//Setting the sun to the right position
var time = (System.DateTime.Now.Hour - 6);
transform.rotation.eulerAngles = new Vector3(15 * time, 0, 0);
}
function Update () {
transform.Rotate(rot * Time.deltaTime, 0, 0);
}
This should fix it. If there is another error comment and I will look at it again.
I just notice another error. Because you need to keep updating the position you need to put "rot = 15 / (hoursInDay 60 60 / 24);" inside the Update function so it will change every frame. If you put it in the Start function it will only be called one time when the script is started.
Welcome. I was shock no one gave you an answer sooner due to how common of a mistake this is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does my player accelerate and not stay constant 1 Answer
Top Down 3D moves in unexpected ways. 1 Answer
Problem with collecting plants 0 Answers
newbie 2d simple sprite rotation 1 Answer