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 Dark_Wolfized · Jun 25, 2014 at 06:03 AM · gui textglobal variable

Global Variable help

I am trying to get a UI counter to work off a global variable since there are several items that will be connected to this script.

I need it to add one to the counter every time one of the items is destroyed.

So far my UI script looks like this (it is very messy, I know)

 static var Counter : int = 0;
 
  guiText.text = " " +Counter;

and this is the code for thr items so that they get destroyed and my attempt to add a global variable to it.

 static var Counter : int = 0;
 
 function OnTriggerEnter (){
 Counter = Counter + 1 ;
 Destroy(gameObject); //Destroys the object the script is attached to.
 }

what do I need to do to make this work

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
1

Answer by fafase · Jun 25, 2014 at 06:06 AM

This is a case of reviewing what s static and instance member. Static belongs to the class and not the object, so when you destroy the object and the component attached to it, the static variable remains in memory with the given value.

So you could go with two scripts.

Counter.js

 static var counter:int = 0;
 // GUI stuff using counter

OtherObject.js

 function OnTriggerEnter (){
     Counter.counter++ ;
     Destroy(gameObject); 
 }
Comment
Add comment · Show 6 · 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 Dark_Wolfized · Jun 25, 2014 at 06:09 AM 0
Share

so what would I need to do then?

avatar image fafase · Jun 25, 2014 at 06:19 AM 0
Share

I changed the idea actually, two scripts is better

avatar image Dark_Wolfized · Jun 25, 2014 at 06:25 AM 0
Share

why

     Counter.counter++ ;


because that doesnt seem to work

avatar image fafase · Jun 25, 2014 at 07:05 AM 0
Share

counter is static, so it is called via the Counter class (Note that I changed the first letter for a lower case). ++ to increase it by one.

Also, this is all depending on your script names. I do not know what are the na$$anonymous$$g you are using, you need to change it if your class is named different.

avatar image Dark_Wolfized · Jun 25, 2014 at 07:28 AM 0
Share

its now co$$anonymous$$g up with the error:

Operator '++' cannot be used with an expression of type 'Object'

Show more comments

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

How to define component properties globally 1 Answer

Embed icons in GUI text?? 1 Answer

Output text GUI not working 2 Answers

Camera following a moving object 0 Answers

How to create a GUI Text when reaching a point 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