Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by stefanlaza · Sep 09, 2013 at 11:21 AM · c#timestorehighscoresnonstatic

how to access a non static timer to store in highscore

 using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
     public float timer = 0f;
     public string time ;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
      timer +=Time.deltaTime;
      time = timer.ToString("0.00");
         
     }
 }


This is the Timer.cs. Now i want to store the time in GameState.cs

 using UnityEngine;
 using System.Collections;
 
 public class GameState : MonoBehaviour {
     // Use this for initialization
     public string highscoretime;
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         highscoretime = Timer.time;
     
     }
 }

But it says an object reference is required to access Timer.time? Timer is attached to a guitext and gamestate is attached to an empty game object. Thanks in advance.

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
1

Answer by aldonaletto · Sep 09, 2013 at 11:42 AM

In this case the easiest solution is to have a reference to the script Timer and assign it in the Inspector: just drag the GUIText object from the Hierarchy to the field Timer Script in the Inspector (or select it with the "dot button"). This field corresponds to the public variable timerScript in the modified GameState script below:

 using UnityEngine;
 using System.Collections;
  
 public class GameState : MonoBehaviour {
 
     public string highscoretime;
     public Timer timerScript; // drag the GUIText here from Hierarchy
 
     // Update is called once per frame
     void Update () {
        highscoretime = timerScript.time;
     }
 }

This is based on a nice Unity feature: declaring a public variable as type Timer allows it to accept dragging any object that has the script Timer attached - the variable actually stores the reference to the script Timer added to that object, not to the object itself.

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 stefanlaza · Sep 09, 2013 at 11:55 AM 0
Share

Thank you very much it worked!! .Just dragged timer to game object--->game state script.

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fade in AudioLister over time #c 1 Answer

Spawning game Object after a period of time 2 Answers

Getting a Code to Run at a Specific Time of Day Even if App is Off. 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