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
0
Question by TechedEd · Jun 11, 2014 at 04:04 PM · valuesaccessing from any scriptstatic-vars

Is there a way to edit a static variable

I know a static variable means that the variable is set, but I need the variable to change and still be accessible from another script.

the variable is:

 var HEALTH = 0;

I know the static variable is:

 static var HEALTH = 0;

I just need the variable to be accessible through another script and at the same time, I need it to be able have different values.

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
1

Answer by Jeff-Kesselman · Jun 11, 2014 at 04:07 PM

"I need it to be able have different values."

No. This is the opposite of what a static variable is

http://en.wikipedia.org/wiki/Static_variable

What you are probably looking for is a public variable, Public variables can be accessed by other objects (components.)

In order to do so you need to first find the game object that has the component on it http://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

Then get the component from it: http://answers.unity3d.com/questions/234883/gameobjectgetcomponentscriptenabled-true-not-worki.html

Then access the public using .

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 tanoshimi · Jun 11, 2014 at 05:27 PM 1
Share

You're getting confused between a static variable and a constant. A static variable can have different values - what makes it static is that it's a member variable of a class rather than of an instance of an object.

However, you're correct in saying that static variables are NOT the correct solution to accessing variables from another script.

avatar image
1

Answer by LMan · Jun 11, 2014 at 04:14 PM

Static variables are not the best way of accessing variables from other scripts. Have a look at this page from the docs.

Static variables don't belong to any one object- this is why health isn't a good candidate for a static var. Points might be a better example- there will only ever be one instance of it, many scripts access it, and it doesn't really belong to any one object.

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

24 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

Related Questions

Unexpected values at the inspector when initializing member values from built-in arrays 0 Answers

How will i update my table values every time? 1 Answer

Animator doesn't recieve values in collisions 0 Answers

Dictionary doesn't exist? 1 Answer

Triple Slider control - UI slider value control 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