Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 FlippingFlopper · Apr 30, 2018 at 06:25 AM · static variable

accessed to a static variable but not getting it's value.

script #1

 public class StageManager : MonoBehaviour {
     public static bool stageActive = true;
 }



script #2

 public class SpawnManager : MonoBehaviour {
     void Update () {
         if (StageManager.stageActive) {
             SpawnEnemy();
         }
     }
 }


in sciript #2, SpawnEnemy() is not operating and it doesn't make any error message. Am I doing it wrong in calling static variable? :( or should I call #1 with GetComponent?

Comment
Add comment · Show 5
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 fafase · Apr 30, 2018 at 07:59 AM 1
Share

Can you print something in SpawnEnemy? Also, this is going to spawn enemy each frame so you get 50 to 100 of them per second.

avatar image the_genius · Apr 30, 2018 at 11:48 AM 0
Share

To add to what fafase said you shouldn't need to use GetComponent. Also, make sure you have your Stage$$anonymous$$anager script attached to a gameObject. Static variables will only work if there is at least one instance of the class in the scene.

avatar image Bunny83 the_genius · Apr 30, 2018 at 01:12 PM 0
Share

That's nonsense. Static variables are not bound to any instance at all. They do not belong to an instance of a class. They belong to the class itself (or to the static context of that class). Whenever you access a class type for the first time the static field initializers and the static constructor will be called automatically. At this point you do not have a single instance of the class.


It's much more important to have the Spawn$$anonymous$$anager actually attached to an active object in the scene.


Of course since Stage$$anonymous$$anager is derived from $$anonymous$$onoBehaviour it seems to be intended as component. though at the moment it doesn't have any instance members so it's irrelevant if you attach it to an object or not.

avatar image TreyH the_genius · Apr 30, 2018 at 01:22 PM 0
Share

You can check this in all of 10 seconds: http://rextester.com/HWSSY24326

avatar image TreyH · Apr 30, 2018 at 01:19 PM 0
Share

SpawnEnemy could be empty or behaving normally. Can you at least post what it does?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlippingFlopper · May 01, 2018 at 01:00 PM

After few hours of sticking in front of monitor to check my scripts, I found out that It was looping from other scripts which is making stageActive variable to be false. So I learned that frequent usage of static variable can be problem if I can't remember where all of them are :p. Thank you for your replies, guys !.

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

82 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

what is better using dontdestroyonload or making the variable static ? 0 Answers

Using static methods to transform a position 1 Answer

Static variable for accessing global object? 3 Answers

Scripts/buttons getting old (prefab?) values of variables 1 Answer

Accessing Static variable from another class gives null value! 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