Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
10
Question by NathanDaniels · Mar 05, 2011 at 05:17 PM · variableglobal

How to make a global variable in Unity?

im trying to make a global variable so other scripts can do stuff with it. How do i make a variable global?

edit using java

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

5 Replies

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

Answer by AngryOldMan · Mar 05, 2011 at 05:20 PM

I'm pretty sure this has already been answered but you can always put

static var

infront of something and that makes it accesible from other scripts.

Comment
Add comment · Show 3 · 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 Nercoe · Sep 26, 2012 at 08:28 AM 0
Share

So simple but thank you!

avatar image UNDERHILL · Jul 09, 2014 at 03:37 AM 0
Share

does this still work it doesn't seem to do you have to put it in both scripts i tried that too and it didnt work

avatar image vickygroups · Jul 09, 2014 at 03:49 AM 1
Share

you don't need to put the definition both places. in c#, you would write static in front of the variable, then in the other script you can access by typing

origscriptname.variablename

avatar image
15

Answer by cregox · Sep 04, 2013 at 12:15 AM

Use `Singleton`s!

They're **much** better than using static.

Grab the singleton script above and simply use it as such:

 public class MyClass : MonoBehaviour {
     void Awake () {
         Debug.Log(Manager.Instance.myGlobalVar);
     }
 }

Manager.cs

 public class Manager : Singleton<Manager> {
     public string myGlobalVar = "whatever";
 }

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 HamFar · Nov 19, 2015 at 12:25 PM 0
Share

Hi Cawas, How do I do this in some if statement in the Update method? I have the following Update and LateUpdate, and I would like to use the latest results of Update in LateUpdate; i.e. be able to use currentPose and target$$anonymous$$odel in the updateAgainLater method. How do I store the currentPose and target$$anonymous$$odel contents immediately, so I can use them in LateUpdate?

 void Update()
 {
     Vector3[] latestPositions;
     Quaternion[] latestOrientations;
 
     // Get the pose data in one call.
     if (Actors.getLatestPose(actorID, out latestPositions, out latestOrientations))
     {
         // Update both the pose and the model.
         updateActor(currentPose, latestPositions, latestOrientations);
         update$$anonymous$$odel(currentPose, target$$anonymous$$odel);
     }
 }
 
 void LateUpdate()
 {
     updateAgainLater();
 }

Thanks

avatar image
8

Answer by jashan · Mar 05, 2011 at 05:20 PM

You can use the "static" modifier. In C#, that would be

public static int myInt = 0;
Comment
Add comment · Show 2 · 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 Jadiker · Feb 07, 2016 at 04:31 PM 2
Share

It took me forever to find this out, so I thought I would mention it here.

If you create a global variable called myGlobal inside a class called GlobalVars, you access that global variable with GlobalVars.myGlobal.

avatar image Pro-Odermonicon · May 15, 2017 at 02:34 AM 0
Share

jashan that worked perfectly! gg.

avatar image
1

Answer by stpharis · Jul 26, 2017 at 04:19 PM

Hi,

This tutorial explains very clearly and simply how to create global variables using Singletons!

https://www.youtube.com/watch?v=CPKAgyp8cno

Hope it helps!

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
avatar image
0

Answer by himanshugupta159 · Jan 25, 2019 at 07:38 AM

Written a blog related to the question which gives the indepth information related to varible scope. link:https://unfragilecoding.blogspot.com/2019/01/variable-scope.html

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

9 People are following this question.

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

Related Questions

Can you make a global reference to a rigid body? 0 Answers

How to use global variable to make counter 1 Answer

Or If statements 1 Answer

Generate a variable for each object that collides 2 Answers

C# - Cannnot access variable in another script unless I get the component everytime. 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