Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ronronmx · Jul 24, 2011 at 09:36 PM · functionstaticsingleton

using non static variable in static function

Here's my problem:

I want to use a normal variable within a static function.

Of course this doesn't work unless the variable itself is static as well. My workaround so far has been to create a copy of that variable as a static variable, then assigning the normal variable to the static version in in Start or Update, and using the static version in my static function.

 public float myNormalVar;
 public static float MyStaticVar;
 
 void Start() {
 MyStaticVar = myNormalVar;
 }


The other workaround I tried was turning my class into a singleton, and using a static property to return an instance of my normal variable:

 public float myNormalVar;
 public static float MyStaticVar { get{ return instance.myNormalVar; } }

But I don't want to turn this class into a singleton, there's never gonna be 2 instances of that class so using static variables works perfect for it.

I was wondering, am I doing this the right way or is there another way to use non-static variables within static functions?

Thanks for your help!

Stephane

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

1 Reply

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

Answer by Waz · Jul 25, 2011 at 12:04 AM

A singleton is the way to go. You list a lot of contradictory "I want" without saying why, so it's hard to give more advice. If your reason for wanting to use a normal variable is so that it shows in the inspector then a singleton is precisely the correct way to achieve that. It's up to you whether you want the variables that do not need to appear in the inspector to be static or member variables accessed through the singleton.

As for "there's never gonna be 2 instances of that class", well yes, that's always the case for singletons.

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 ronronmx · Jul 25, 2011 at 06:52 AM 0
Share

Thanks for your answer Warwick, and sorry for my vague question!

I guess I still get a little confused on when to use a Singleton and when to use a class full of static variables.

$$anonymous$$y class checks for WheelCollider collisions with the ground, and store the information in multiple static variables which I access from many other classes. For most of those static variables, i do not need to see them in the inspector, but for some, I do need to see them in the inspector but still be able to access them from all other scripts "easily" like a static variable.

So even though my question was vague and missing some information, I think you nailed it in your answer. A Singleton is probably the way to go for my particular issue.

Thanks again!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using static properties that return _instance.variable... 0 Answers

Static function and variables error 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

variable = true from another script 2 Answers

how to make function parameters don't need to declare (custom parameters)?? 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