• 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 /
avatar image
0
Question by Friescest · Aug 08, 2014 at 11:56 AM · variablepassing

Passing a constantly updated variable to another class

I have problems passing the variable gameTime from a class SetGameTime to DayNightCycle. I can get the start value of gameTime (which is 9000) in the class DayNightCycle, but I would like to get the updated value of gameTime in that class (I keep adding to gameTime). To get gameTime I request the variable from SetGameTime using:

 gameTime = SetGameTime.gameTime;

I could also run a method in DayNightCycle from SetGameTime. This gets me the updated version of gameTime in the DayNightCycle class, but not in the Update method. What should I do to get gameTime in the Update method of DayNightCycle?

The SetGameTime class:

 using UnityEngine;
 using System.Collections;
 
 public static class SetGameTime{
 
     public static float gameTime = 9000.0f;
 
     public static float timeSpeed = 50.0f;
 
     static void Update () {
         gameTime += Time.deltaTime * timeSpeed;
 
         if (gameTime > 24000)
             gameTime -= 24000;
     }
 }


The DayNightCycle class:

 using UnityEngine;
 using System.Collections;
 
 public class DayNightCycle : MonoBehaviour {
     //Angular speed of the sun
     public float timeSpeed = SetGameTime.timeSpeed;
 
     //In-game time in synchronization with the angle of the sun
     public float gameTime = SetGameTime.gameTime;
 
     //Angle of the sun to the horizon
     public float sunAngle;
 
     //Amount that the first and second sky box are blended
     float blendAmount = 1;
 
     public GUIText timeDisplay;
 
     void Start () {
         timeDisplay.text = "The gametime is: " + gameTime.ToString();
     }
 
     void Update () {
         gameTime = SetGameTime.gameTime;
         print ("gametime in daynightcycle is: " + gameTime);
 
         UpdateSunAngle ();
 
         PassToSunIn ();
 
         PassToLightPost ();
 
         PassToDayOn ();
     }
 //more stuff down here, doesn't matter at the moment
 }

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

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Aug 08, 2014 at 11:54 AM

do you intend to call Update() from SetGameTime manually?

this is important because you have not created it as a MonoBehaviour so the usual methods won't be set up/called for you/etc.

why does your game time need to be set outside of the day/night handler? it might make sense to include it in there...

most importantly, in order access the fields/methods in your SetGameTime class, you need to have an instance of it. since it's not a MonoBehaviour, you need to do something like:

 var myGameTime = new SetGameTime();

and then reference everything contained within it something like this:

 myGameTime.gameTime = 9000.0f;
 print("timeSpeed = " + myGameTime.timeSpeed);

also, remember that if you declare the class and any contents as static, there will be only one...

Comment
Add comment · Show 1 · 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 Friescest · Aug 08, 2014 at 02:41 PM 0
Share

I'd like to set my gameTime outside the DayNightCycle because I want a lot of scripts to pull their gameTime value from a single one. I can calculate gameTime seperately in every script but that's bad coding. I made the SetGameTime script static so I don't need a function call like DayNightCycle obj = new DayNightCycle(); obj.ReceiveGameTime(gameTime); This is not necessary though.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

2 People are following this question.

avatar image avatar image

Related Questions

ArgumentException: The Object you want to instantiate is null. 1 Answer

Best way to pass variables between instantiated/colliding objects? 1 Answer

Passing a variable by reference in javascript (using a C# script) 1 Answer

Trying to pass a float value from a prefab to another script that compares values from multiple prefabs 1 Answer

Passing position to function 1 Answer


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