Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
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 this so far. What changes would I need to make to get this 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 this 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 things 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).


Thirdly, 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 this, see HERE for a safe way to do this).

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.

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

One of my scripts can't find the variable from this script 1 Answer

How to use real life timer 1 Answer

Help with variables 2 Answers

Is it possible to save a transform.position in a single variable? 1 Answer

Help with GUIText health bar please 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