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 /
This question was closed Jul 02, 2017 at 01:40 AM by Rdubb for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Rdubb · Jun 12, 2017 at 01:06 AM · multiple objectsaccessing scriptsglobal variable

Multiple objects writing to a single variable on another object's script.

Hello,

I have a number of objects on the play screen, each of of which has an "on" and "off" state (dependent on sprite and tag). States are affected by collision from a single ball object. When the state turns to "on" (after colliding with the ball while previously "off"), I want it to increment a global score count by 1. The count can also be decremented by clicking an "on" object, to make it "off." When the count reaches a certain number, I want it to load the fail screen.

At the moment, I have a global scoring script on the Ball, with all of the other objects accessing the script in their respective Start methods. When collision happens, the involved object accesses the Ball's "count" variable and increments it by 1. My problem is that each object is writing it's own individual "count" variable, rather than writing to the global variable on the Ball, and ignoring the fail code completely. My code below:

Code on the Ball (global scorer):

 public float count = 0;
 
 void Update () {
         if (count == 8)
             SceneManager.LoadScene("Fail");
     }

Code on each on/off object:

 void Start () {
         float count = GameObject.Find ("Ball").GetComponent<globalscorer>().count;
     }
 
 void ChangeSprite () {
     
         if (spriteRenderer.sprite = dark) 
         {
             spriteRenderer.sprite = bright;
             transform.gameObject.tag = "on";
             count = ++count;
 
     void OnMouseDown () {
 
         if (spriteRenderer.sprite = bright) {
             spriteRenderer.sprite = dark;
             transform.gameObject.tag = "off";
             count = --count;
             }

I'm sure I'm missing something fundamental so any help would be appreciated. Ideally, I would like all scoring to be tracked in a single variable on one object, with all other object states informing that variable.

Thanks for any help!

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

  • Sort: 
avatar image
0
Best Answer

Answer by Rdubb · Jul 02, 2017 at 01:37 AM

Ended up solving this on my own. Making "count" a public static float allows other scripts, including ones on other objects to access and change the count. In the case of the above, the difference would be:

In the GlobalScorer script

 public static float count = 0;

On each object's on/off script

 ++GlobalScorer.count

As long as the variable is a public static, using . in any script will reference that particular one. Hope that helps anyone dealing with a similar issue

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 Steve-Tou · Oct 13, 2018 at 06:47 PM 0
Share

Thanks!! This was a very simple answer that I have been looking for for several weeks. $$anonymous$$eep this posted for us hobbyists (noobs).

Follow this Question

Answers Answers and Comments

107 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 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

How do I pickup multiples of the same item with the same tag? 1 Answer

How to use objects differently between scenes? 0 Answers

HOW TO.. Grouping gameobject and move object in arraylist (Runtime) 0 Answers

Creating Modular Firing Mechanism 1 Answer

Same Coroutine on multiple objects 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