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 RoR · Oct 23, 2010 at 06:26 PM · globalvariables

Global Variables

I have a question regading global variables.

Am I allowed to define a

GlobalVariable inside of my GameSystem class? Such like in the "Player Class"

He has a inventory. Can I make this inventory:

public static Ilist name = new IList();

Making this static will allow me to access this inventory bag from any other script correct?

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

3 Replies

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

Answer by boymeetsrobot · Oct 24, 2010 at 08:44 AM

You are on the right track with the static variable. Just remember that a static var is a class based var as oppose to an instance of the class. Because of this you would access it via the class name instead of the instance name:-

public class GameManager : MonoBehaviour {

public static float myStaticFloat;

And in your class that wishes to access it you would do:-

GameManager.myStaticFloat = 1.5f;
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
1

Answer by _Petroz · Oct 23, 2010 at 11:11 PM

Given you asked a another question regarding architecture, I will offer an alternative approach because generally global variables are a sign of bad architecture. Reducing the scope of variables reduces the complexity of code.

You could make the inventory internal to the Player and make it public, or better yet private and accessed via property. Any object which needs to access the player inventory should have access to the player.

If they don't you could always take the quick way out and use GameObject.Find, although that relies on global state, it's an improvement over introducing additional global state to your game.

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 Bravini · Oct 23, 2010 at 06:29 PM

exactly, you will reference it through scriptname.name(); as a good practice, I recommend you define static vars with all capped name ie: MYSTATICVAR so it's easy to see wether it's global or local.

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 RoR · Oct 23, 2010 at 06:51 PM 0
Share

When you say reference it through scriptname.name().

I don't quite undersatnd what you mean. Do I setup a method/function to return the variable? Then in my other script I do a method call to return the object to the other script?

avatar image RoR · Oct 23, 2010 at 06:52 PM 0
Share

Also since name is a list. I don't quite understand scriptnam.name()?

Name is not a method/function? Am I missing something here? Or is this Unity magic with global vars allowing me to do .name()?

avatar image Bravini · Nov 17, 2010 at 06:17 PM 0
Share

when set to static, you can access an attribute directly through the script name, as if it where attached to the object.

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

No one has followed this question yet.

Related Questions

Make a global variable that can be accessed in different scenes 2 Answers

null refference exception. right after i dimention the object array?? 2 Answers

Global variables with fixed shaders 0 Answers

How do i create a global structure and how do i acces it from any script? 3 Answers

Google Mobile Ads Unity Plugin - global bannerView 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