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
3
Question by fishZombie · Mar 15, 2015 at 07:10 PM · textpublic variablecs0103global script

How do you make a global variable, global in Unity?

     public float PathLength = 0f;
     PathLength = PathLength + SegmentLength;

This gives me the correct data I want, displayed in the Inspector. I want it displayed in the HUD.
So In My HUDScript I wrote this:

     mytext.text = "Line Length: " + PathLength;

I get this error:

error CS0103: The name `PathLength' does not exist in the current context

Comment
Add comment · Show 1
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 MagicZelda · Mar 15, 2015 at 07:35 PM 0
Share

if the script

public float PathLength = 0f; PathLength = PathLength + SegmentLength;

is in another script on another gameobject you will need to get a reference to that GameObject

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by raulrsd · Mar 15, 2015 at 07:43 PM

When I want a really global variable what I do is to make a static class to store this variables, like this:

 public static class GlobalVariables{
      public static float pathLength; 
 }

This way every single script in my game could access this variable just writing:

 GlobalVariables.pathLength = 1.0f;
 
 GlobalVariables.pathLength += SegmentLength;

Or whatever.

Comment
Add comment · Show 5 · 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 fishZombie · Mar 17, 2015 at 02:19 PM 0
Share
     mytext.text = "Line Length: " + PathInput.pathLength;


ugg.... now I'm getting this:

error CS0117: PathInput' does not contain a definition for pathLength'

Is it a problem with the static class? $$anonymous$$ine is not (can not be static. its attached to different units).

avatar image Denvery · Mar 17, 2015 at 02:22 PM 0
Share

Tell more about class PathInput or post it's text.

avatar image MagicZelda · Mar 17, 2015 at 02:24 PM 0
Share

can u show us PathInput class

avatar image DiebeImDunkeln · Mar 17, 2015 at 02:30 PM 0
Share

if your public varialble is in a script with name for example globalScript.cs then write in the script other script where you want to use this:

 globalScript.PathLength 

And in globalScript.cs you have to write:

 public static float PathLength = 0f;
 PathLength = PathLength + SegmentLength;

The globalScript class itself don't need to be static.

avatar image Bonfire-Boy · Mar 17, 2015 at 02:35 PM 0
Share

You don't have to make a static class to have a static variable. If a static variable is associated with a particular class, I$$anonymous$$O it makes more sense to make it a member of that class. So in your case a quick way to do what you're after is just to make PathLength a public static member of PathInput.

But is that really what you're after? The example you give suggests that you don't actually want PathLength to be global or static, you just need to be able to access it from a different class. In that case you may be better off following $$anonymous$$agicZelda's advice and access PathLength through a reference to the PathInput you're concerned with.

BTW your "does not contain a definition for pathLength" may simply be to do with the first letter being capitalised or not.

avatar image
0

Answer by UNZoOM · Mar 20, 2015 at 06:29 PM

 // MODIFY HUD SCRIPT
 
 public var GiveMePathLengthGO: GameObject; // Drag the GO here which has the script attached to it which has the pathLength variable Initialized.
 
 function Start()
 {
  
 }
 
 function Update()
 {
  // You can now use the pathLength like so,
 mytext.text = "Line Length: "+GiveMePathLengthGO.GetComponent(PathLengthScript).pathLength ; 
 }

Note : PathLengthScript is the script name where you have path length being initialized or wherever you would like to call it from. This is a workaround , the most ideal way to use is Static class as raulrsd mentioned.

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 Invictum · Mar 20, 2015 at 11:27 PM

You actually should use a singleton if you really need a global.

http://wiki.unity3d.com/index.php/Toolbox

Toolbox is a slightly improved singleton for unity you can just grab the code for the classes and add, then make your global in toolbox cs. It's just as easy, you can call Toolbox.Instance.Myglobals from anywhere. But it is a much better practice if you "need" a global or two.

While there is great debate as to not use singletons at all or not I think they all agree the old static poor practice. I still find singletons useful so I am of the lazy side of the argument I suppose.

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 Blenderik · May 05, 2016 at 08:17 AM 0
Share

@Invictum Hi I hope this thread is not too old. But the toolbox uses singletons and according to $$anonymous$$SDN they are outdated. About the static classes: I did this with a bunch of variables, but they (apparently) get garbage collected at some point. It's the only explanation I can come up with, when I get the message value not assigned at random times. Is there a way to protect a certain class from garbage collection or is there another possible reason for this? BTW the static class was not a $$anonymous$$onoBehavior, because I didn't want to have to attach it to a GO

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

10 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

Related Questions

Why can't I drag text into public text in the inspector when instantiated? 1 Answer

Andoid enter text without gui 1 Answer

Mirrored text for mirror projection 0 Answers

How to save script component variables at runtime from IOS device? 1 Answer

Is it possible to italicise GUI text without having italic font? 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