Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by cmos · Feb 15, 2010 at 01:13 AM · variable

Global Variable accessible from scripts within scene

Hi

I would like to have a global variable that I can access from all scripts in a scene edit / update it.

Any ideas?

Thanks, Chris

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Sebas · Feb 15, 2010 at 01:45 AM

You can find such information in the Script Reference under the section "Global Variables" (Link to Script Reference)

Comment
Add comment · Show 4 · 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 cmos · Feb 15, 2010 at 02:17 AM 0
Share

Thanks for your response. Is there a way to reference a Label value from another script? I created a GUI label with something like this: GUI.Label (Rect (100, 25, 100, 30), text) .... how can I retrieve the value stored in it from another script?

avatar image Sebas · Feb 15, 2010 at 02:34 AM 0
Share

answered/edited this in your other question. see this answer http://answers.unity3d.com/questions/4009/how-to-make-a-timer-in-the-game

avatar image Sebas · Feb 15, 2010 at 02:36 AM 0
Share

quick answer here: the label value is nothing but a normal string variable. You simply declare the variable at the beginning of your script as member variable (outside of any functions) and access it through other scripts like described in your other question which I linked in the previous comment.

avatar image azebramoomoo · Dec 19, 2021 at 03:21 AM 0
Share

this link seems to be broken. was the way it worked changed by an update?

avatar image
2

Answer by Ashkan_gc · Feb 15, 2010 at 05:41 AM

you have two ways to do this. first of all you can access public variables of scripts from other scripts using GetComponent like what sebas said. also you can create a static class and create static variables in it and use them anywhere in your game. simply create a static class and use it's name to get a reference to it.

static class SceneData
{
static int width=100;
static GameObject soundManager;
}
//usage
void Start ()
{
SceneData.soundManager=GameObject.find ("sound");
}
void Update()
{
SceneData.width=time.time;
}

you can use sceneData class in any script in any gameObject.

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

1 Person is following this question.

avatar image

Related Questions

Passing position to function 1 Answer

Keeping a destroyed game object afterwards 0 Answers

Hide public field on base class but show it on derived class 1 Answer

How to properly declare this variable 2 Answers

How do you clear a variable in the inspector? 4 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