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
1
Question by skine · Jul 03, 2012 at 03:44 PM · staticvar

How to use a static var

Hi guys. I am new to Unity and help is greatly appreciated, I have a tiny problem where I have a script with two variables each holding a game object (I am trying to make it invisible) now I am trying to hide these two gameobjects through the use of a different script, so I tried making the variables static, but once that is done the variables dissapear in the unity window and I can't drag a gameobject in anymore.

var bodyPart1 : Transform;
var bodyPart2 : Transform;

These are the ones I am trying to make static, and then in a different script I am trying to access it like so

HealthControl.bodyPart1.renderer.enabled = false;

, but like I said as soon as I make them static, I can't drag a gameobject in the variables and it comes up with an error as the var becomes empty (null)

Any ideas?

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
2

Answer by dannyskim · Jul 03, 2012 at 05:25 PM

This is done on purpose. Static members of a class are global variables, and thus should not in practice be editable outside of the class, or without calling the class directly.

In normal programming, you typically don't want to use static variables because this can lead to improper accessing of said variable, which in turn could cause problems down the road. Typically, you'll want to make a variable public or private, and access it by creating a new instance of the class itself, and changing the property by accessing that instance of the class, which is one of the reasons they call it Object Oriented Programming.

In any case, like I said static variables are meant to not be accessed by the inspector. You have to set it before or during runtime in order to set it's value, with something like GameObject.Find(), Resources.Load(), etc. Do so in Start() or Awake() to initialize it, and then you can call it just fine.

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 skine · Jul 03, 2012 at 05:34 PM 0
Share

Thanks for your answer, I don't really understand the Object Oriented but I guess I could predefine them within the code ins$$anonymous$$d of the inspector thanks! Any more input on what you said about making a new instance of the class and how to do that would be great as I have to be honest I don't really understand it too well.

avatar image
0
Wiki

Answer by f34rtehninja · Jul 03, 2012 at 08:42 PM

So I think you're trying to make other game objects invisible from a controller/manager type object, right?

Static variables would probably not be the way to do this, static variables are global variables that persist throughout changes in the game, it's good for things like score or player health as the player travels through several different scenes. More on global variables can be found here at the very helpful scripting reference Scripting Reference Global Variables.

What I believe you should do is instead get a reference to the object you're intending to make invisible and then access the renderer that way. You can do as follows:

 var bodyPart1 : GameObject;
 var bodyPart2 : GameObject;

Then save the script, attach it to your controller, drag the references over in your inspector then the following will work inside your health controller script:

 bodyPart1.renderer.enabled = false;
 bodyPart2.renderer.enabled = true;

The renderer component is part of the GameObject, you were trying to do it via the transform which wouldn't work.

If you wanted to access the health controller script from elsewhere you could make an accessor function in the health control script, then get a reference to that script and call the function.

 var HealthControl : HealthControlScriptName;
 

Then drag a reference with the inspector and inside the script that accesses the HealthControlScriptName the make a function call like

 HealthControl.HideBody1(true);
 HealthControl.HideBody2(false);

and inside of the HealthControlScriptName you can fill out the functions like:

 public function HideBody1(hideIt : Bool)
 {
    bodyPart1.renderer.enabled = !hideIt;
 }

 public function HideBody2(hideIt : Bool)
 {
    bodyPart2.renderer.enabled = !hideIt;
 }

This, when called, will hide or unhide the body part. Do you want to hide bodyPart1? Call HideBody1(true). Unhide it with HideBody1(false). The public keyword is what you intend to do with static, I believe. It allows access through other scripts when you have the reference to the script that holds the public functions.

Make sense? This is what you needed?

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

7 People are following this question.

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

Related Questions

Make Score Go Up At a Collision 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Accessing a js static var from a c# script 1 Answer

Health is set to zero at start of game. 3 Answers

Static variables explanation. 2 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