• 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 NinjaBryden88 · Feb 07, 2018 at 06:10 PM · variablesvar

Trouble accessing another script's variables. Alternative to using var for accessing variables from other scripts?

I am attempting to make a game manager that handles all variables in a simple game I am making and am currently testing it on a timer. However, the var keyword is the only issue I am coming across regarding t$$anonymous$$s so far. What changes would I need to make to get t$$anonymous$$s to work? Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class timer : MonoBehaviour
 {
     public Slider Timer;
     var gameManager;
     // Use t$$anonymous$$s for initialization
     void Start ()
     {
         Timer = GetComponent<Slider>();
         GameObject gameManager = GameObject.Find("GameManager");
         Functions functions = gameManager.GetComponent<Functions>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         gameManager.startTime -= Time.deltaTime;
         Timer.value = gameManager.startTime;
 
         if (gameManager.startTime <= 0)
         {
             Timer.value = 0;
             gameManager.startTime = 0;
         }
     }
 }
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 Commoble · Feb 07, 2018 at 06:49 PM

You're on the right track, I don't like using the var keyword either. Here's a few t$$anonymous$$ngs I see:


Firstly, if you need a reference to the gameobject itself, I would use GameObject gameManager; instead of var gameManager;.


Secondly, if you need a reference to a SCRIPT on that gameobject (like your Functions class), I would keep a reference to the script instead of a reference to the gameobject (if you need both, I would still keep a reference to the script, as it's faster to get a gameobject from the script reference than it is to get a script from the gameobject reference).


T$$anonymous$$rdly, in your update function, you're trying to access gameManager.startTime. Your gameManager field currently holds a GameObject. GameObjects don't have any field called startTime. If that's a field from your Function script, you should be accessing startTime from your Function script instead of from the gameObject (use functions.startTime, if you have such a field).


Fourthly, it's generally considered bad form to use GameObject.Find() except as an absolute last resort (and even then it's considered bad form to put yourself in a position where it's your last resort). If you have a game management script that you know you will only ever have one instance of, a better alternative would be to put a static field containing the only instance of it, e.g. public static Functions instance; in your Functions script (there are also bad ways to do t$$anonymous$$s, see HERE for a safe way to do t$$anonymous$$s).

Comment
Add comment · 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

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

76 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 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 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 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 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

Is it possible to create new variables by incrementing? 1 Answer

Which variable can be implicitly declared? 1 Answer

Accessing a js static var from a c# script 1 Answer

How i "import" variables? 2 Answers

Import Variables from another active script 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