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 /
  • Help Room /
avatar image
0
Question by georgesmuga · Feb 10, 2017 at 07:39 PM · variablestatic

can't access a public static int from another class. Value is always zero

In the class Spawner, I need to access the int L1score, which is declared as public static in the class HeroController. But when I access the value, as 'HeroController.L1score', the return value is always zero.

Here's the declaration in HeroController:

 public class HeroController : MonoBehaviour {
 
     public static int L1score = 0; //need this to report in Spawner
 
     public void UpdateScore () {
         GUIpoppedBubbles.text = "Popped: " +score;
         L1score = score;
         Debug.Log("HeroController; UpDateScore: "+L1score);
     }

here's Spawner:

 public class Spawner : MonoBehaviour {
 
 int x = HeroController.L1score;
 
 Debug.Log ("Spawner:EndGame. HeroController.L1score is " + x);
 }

the output in the console: HeroController; UpDateScore: 77

Spawner. HeroController.L1score is 0

Comment
Add comment · Show 1
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 HenryStrattonFW · Feb 10, 2017 at 09:00 PM 0
Share

Could you post your entire spawner class? if the spawner is logging after the hero controller then there must be something else settings L1Score to 0. is there any other code that sets a value on this or is UpdateScore() the only place?

2 Replies

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

Answer by karadag · Feb 10, 2017 at 10:05 PM

Herocontroller.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HeroController : MonoBehaviour {
 
     public static int L1score = 0; //need this to report in Spawner
 
 
     void Update()
     {
         UpdateScore (20);
     }
 
     public void UpdateScore (int score)
     {
         L1score = score;
         Debug.Log("HeroController; UpDateScore: "+L1score);
     }
 }

Spawner.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour 
 {
 
     int x;
 
     void Update()
     {
         TestFunction ();
     }
 
     void TestFunction()
     {
         x = HeroController.L1score;
         Debug.Log ("Spawner:EndGame. HeroController.L1score is " + x);
     }
 }
 
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 SuperRyn · Jan 31, 2019 at 03:26 PM 0
Share

This helped me XD

avatar image
0

Answer by georgesmuga · Feb 11, 2017 at 06:15 PM

Thanks HenryStrattonFW -- my Spawner and HeroController run simultaneously. The Spawner puts out the targets and the HeroController shoots them down.

karadag has the right answer. Apparently, the UpDate method needs to know about the static variable, at least in my case. I simplified karadag's answer a little, and the variable shows up where I need it:

public class HeroController : MonoBehaviour {

  public static int L1score = 0; //need this to report in Spawner

void Update()

 {
     L1score = score;
 }

public void UpdateScore ()

 {
     GUIpoppedBubbles.text = "Popped: " +score;
     Debug.Log("HeroController; UpDateScore: "+L1score);
 }
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 karadag · Feb 11, 2017 at 10:39 PM 0
Share

If my answer correct please select my message for correct answer. Thanks

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

Change of static variable 1 Answer

,Changing the variable on only one object 0 Answers

What are some best practices for static variables in multiplayer? 0 Answers

How to make a global variable in Unity? 5 Answers

[Help] Getting variable based on string value. 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