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 swifteria · Feb 22, 2015 at 04:06 PM · c#staticint

Static Integers Problem

Hi! I have here these two scripts:

Script A - This goes on the Tree object.

 public class CutTree : MonoBehaviour 
 {
     public AudioClip ChopSound;
     public GameObject Tree;
 
     public static int amountHitTree;
     
     void Start()
     {
         Tree = this.gameObject;
         amountHitTree = 0;
     }
     
     void OnMouseOver()
     {
         if(Input.GetMouseButtonDown(0))
         {
             _Manager.Wood += 1;
             audio.PlayOneShot(ChopSound);
             amountHitTree += 1;
         }
     }
 
     void Update()
     {
         if (amountHitTree == 5)
         {
             Destroy(gameObject);
         }
     }
 }

and Script B - This goes on an empty gameObject.

 public class _Manager : MonoBehaviour 
 {
 
     public static int Wood;
     public static int Stone;
     public static int Meat;
 
     public AudioClip treeFallSound;
     private bool hasPlayed = false;
 
     void OnGUI()
     {
         GUILayout.Label("Wood: " + _Manager.Wood);
         GUILayout.Label("Stone: " + _Manager.Stone);
     }
 
     void Update()
     {
         if(CutTree.amountHitTree == 5)
         {
             if(!hasPlayed)
             {
                 audio.PlayOneShot(treeFallSound);
                 hasPlayed = true;
             }
         }
     }
 }
 

I have the 'amountHitTree' int set to static because of sound issues, and a OneShot playing an infinite amount of times every frame. The problem is that when I hit one tree 5 times, it destroys all the others. I know why; this is because the 'amountHitTree' int is static--is there any other way i can fix this?

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 gjf · Feb 21, 2015 at 04:34 PM 1
Share

then don't make it static!! if you've got another problem, then fix that separately.

what does "...set it to static because of sound issues" mean?

making it static means that every tree instance will reference the same amountHitTree so they will all trigger at the same time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RudyTheDev · Feb 22, 2015 at 04:26 PM

As gjf already said, static means -- shared between all instances of this class. No matter how many CutTrees you make, amountHitTree will always be pointing to the same variable/value. It will be there even if you have 0 trees.

You should generally not use "static" unless you know what you are doing. You could theoretically solve your issue with statics in CutTree, but you shouldn't.

Add public static _Manager Instance; field to _Manager class and add Instance = this; to its Awake() method. This will be a singleton (search around for singleton pattern), a simple proper example of using statics.

Then add public void TreeCut(CutTree tree) { } method to your _Manager. This will be called by any tree that gets cut down.

Then in CutTree make amountHitTree non-static and in Update() when it has been hit 5 times, call _Manager.Instance.TreeCut(this);. Now, every time a tree gets hit 5 times, it will tell this to the manager. The manager can then play sounds or count resources, etc.

In fact, you should probably apply the same principle to counting resources.

P.S. Although not an issue with singleton solution, you have two updates: _Manager.Update() and CutTree.Update(). They can run in any order. This means CutTree may destroy itself before _Manager ever checks it in your version.

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

cannot implicitly convert type 'int' to 'bool' problem 1 Answer

Static int wont change from another script 0 Answers

How to tell if an int is even 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