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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by usef155 · Oct 15, 2015 at 01:46 AM · variableglobalglobal variable

How to use global variable to make counter

I am trying to make a global variable in C# that is modified by two different scripts so that when a game object is collected it adds 1 to the current value of "count" (the name of the variable). For the variable definition I used this line of code.

public static int count;

I applied this to both scripts. The next bit is the collider code.

  void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ( "Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }


Both of the game objects/scripts detect the cubes being counted but only one of them contributes to the variable "count". They both have the exact same bit of code for the collider.

Comment
Add comment · Show 7
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 saud_ahmed020 · Oct 15, 2015 at 06:58 AM 0
Share

$$anonymous$$ake a GameController and declare "count" veriable in GameController Script. Now when you need to use count veriable you must access this through following code

GameController.SharedInstance().count++;

avatar image usef155 saud_ahmed020 · Oct 15, 2015 at 10:42 AM 0
Share

But now whenever I use it to modify the value it says

Assets/Scripts/PlayerController.cs(22,50): error CS1525: Unexpected symbol `='

On both scripts.

avatar image saud_ahmed020 usef155 · Oct 15, 2015 at 01:15 PM 0
Share

Can you share your code here ??

Show more comments
Show more comments
avatar image usef155 saud_ahmed020 · Oct 16, 2015 at 03:47 PM 0
Share

But I need to assign the variable to start at zero. Later in my script it adds to it. I want to able to assign a value to it, how do I do so? Here is the bit of code where I add to the value, how do I do so?

 void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ( "Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }

avatar image meat5000 ♦ · Oct 16, 2015 at 05:16 PM 0
Share

Format your code. Edit your posts, highlight the code and click the 101010 button.

1 Reply

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

Answer by usef155 · Oct 18, 2015 at 09:24 AM

Okay, so I found out how. Here are some little snippets of code explaining the way I did it.

At the beginning of your code you need to create a public class with a name of your choice (I chose Global so I could remember what the class is for). Next you need to make a public static int followed by the name of the variable you want to make global. Below is what I used in my code to make my global variable by the name of "count".

 public class Global 
 {
     public static int count = 0;
 }

Now that we have created a global variable by the name of count with a default value of zero, we need to find out how to refer to that global variable, and how to edit it's value.

Whenever you want to refer to your global variable you would now call it Global.count or whatever you decided to call yours. If your class was Example and your variable name was ExampleVar then you would refer to it as Example.Examplevar in your code.

And now, I am going to show my code where when my player collects/collides with an object it adds 1 to value of my global variable.

 void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ( "Pick Up"))
         {
             other.gameObject.SetActive (false);
             Global.count = Global.count + 1;
         }
     }
 }

As you can see I used and if state so that when the player collides it adds 1.

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

32 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

Related Questions

How to make a global variable in Unity? 5 Answers

Reassigning variable inside OnCollisionEvent does not change variable inside the Update method 0 Answers

Can you make a global reference to a rigid body? 0 Answers

Generate a variable for each object that collides 2 Answers

C# - Cannnot access variable in another script unless I get the component everytime. 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