• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 20, 2015 at 04:26 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Bengooch7 · Jul 20, 2015 at 03:59 PM · javascripteditorbugtime

Why isn't this public variable working? (JS)

Good day, everyone!

I have a public variable in this JS script that for some reason, doesn't affect anything when I change it while running the game. The speedOfLight starts as "1". It is a multiplier for how fast the sun should rotate around the world. If I set it manually in the script, it works. If I set it in the editor, It has no effect on the speed the light moves.

You can attach the code to a directional light to see what it does, if you'd like.

 #pragma strict
 
 public var normalPassageOfTime = true;
 
 //How many degrees the sun rotates per second
 private var degreesOfRotation : float = 360f /*degrees*/ / 24f /*hours*/ / 60f /*minutes*/ / 60f /*seconds*/;
 
 //Visible variable to adjust speed of time flow
 public var speedOfLight : float = 1f;
 
 //Calculation of time flow speed
 private var speedModifier : float = degreesOfRotation * speedOfLight;
 
 //Get Hours, Minutes, and Seconds as a string
 public var currentHour = System.DateTime.Now.ToString("HH");
 public var currentMin = System.DateTime.Now.ToString("mm");
 public var currentSec = System.DateTime.Now.ToString("ss");
 
 //Convert current number of hours/minutes passed since midnight into usable seconds
 //Seconds will just equal current as a float
 private var hoursInSeconds : float = parseFloat(currentHour) * 60f * 60f;
 private var minutesInSeconds : float = parseFloat(currentMin) * 60f;
 private var secondsInSeconds : float = parseFloat(currentSec);
 
 function Awake() {
 //set position of the sun based on time of day
     transform.Rotate(0,0,degreesOfRotation * (hoursInSeconds + minutesInSeconds + secondsInSeconds));
 
 }
 
 function Start () {
     
     
 }
 
 function Update () {
     //rotate the sun based on set speed (Needs Variables adjusted)
     if (normalPassageOfTime == true) {
         transform.Rotate(0,0,speedModifier * Time.deltaTime);
         }
     
 }


It's probably worth noting that if you are watching for the light to move at normal speed, you aren't going to notice it. I have it set up to move at a real time of day rate.

By the way, I'll take this moment to pat myself on the back, as this is the first script I've ever written, and I did it all with my noodle (except for looking up syntax things about JS). Very proud of myself. Roll your eyes at the new guy if you must. ;-)

Is it maybe a bug in Unity that is preventing this from being effective?

Thank you, everyone in the community!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Jul 20, 2015 at 03:45 PM

In uJS leave out the 'f'. You should add the .0 though.

Dont do this in the globals...

 private var speedModifier : float = degreesOfRotation * speedOfLight;

Declare it globally and initialise it in a function.

 private var speedModifier : float; 
 function Start()
 {
     speedModifier = degreesOfRotation * speedOfLight;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bengooch7 · Jul 20, 2015 at 04:05 PM 0
Share

meat5000, Thank you! I moved the initalization inside the if statement running in my Update() and it works now. how do I rate an answer? I don't see an option, and the answers counter at the bottom says "0"...

avatar image meat5000 ♦ · Jul 20, 2015 at 04:20 PM 0
Share

Glad I could help.

You can click the tick to accept an answer on UA.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody.MovePosition doesn't move reliably? 1 Answer

Updating an array indices dynamically 2 Answers

Gravity Switch by button? 1 Answer

Editor Window stuck on a blank white screen 2 Answers

NavmeshComponents baking creates a new navmesh instead of overwriting 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges