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 /
avatar image
0
Question by xDaniTheKidx · Sep 26, 2017 at 02:23 AM · programmingprogramming-basics

Doesn't exist in the current context?

I am working on a scorekeeping system. So far I have a goal on each side of the map. When the "ball" hits the opposing goal, I want the player's score to be shown on the screen.

I have 2 objects in the hierarchy. 1 being GoalP1 and the other being GoalP2. I created a text field and positioned it accordingly. I put this in my gameManager script.

 public class GameManager : MonoBehaviour {
 
     public Text scoreText;
     public static int redScore = 0;
     public static int blueScore = 0;
 
     void Update(){
         scoreText.text = redScore.ToString () + "|" + blueScore.ToString ();
     }
 
 }

And then I put this in the BallController script.

 if (col.gameObject.name == "GoalP1") {
             blueScore = blueScore + 1;
         }
 
         if (col.gameObject.name == "GoalP2") {
             redScore = redScore + 1;
         }

I am not entirely sure what I'm doing wrong but am getting the error that 'blueScore' does not exist in this context. I am a fairly new programmer, so bare with me please.

Anywho, thanks for the help! -DaniTheKid

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by daniel_alves_bra · Sep 26, 2017 at 03:45 AM

redScore and blueScore belong to the GameManager class. So, if you wanna call these attributes in another class you must put first the GameManager instance, and after that you call the atributes.

Ex: on the BallController you can do

 public GameManager gameManager;

 

on the update method you do:

 void Update(){   
     gameManager.blueScore = "something";
     
     gameManager.redScore = "something";
 }

Remember to drag the GameManager to your BallController script.

Comment
Add comment · Show 2 · 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 Bunny83 · Sep 26, 2017 at 03:54 AM 1
Share

The two variables are declared static. So he doesn't need an reference to the instance of the class but can simply do:

 Game$$anonymous$$anager.redScore 

ins$$anonymous$$d of

 redScore
avatar image daniel_alves_bra Bunny83 · Sep 26, 2017 at 04:26 AM 0
Share

Yeah, you are right.

I'm not a C# programmer but It is kinda strange for me to have a static attribute if the value isn't been serialized anywhere. I think a static variable would be more useful if it was created to declare constants...

avatar image
0

Answer by Agent27765 · Sep 26, 2017 at 02:33 AM

I am a fairly new programmer to but I would like to take a try at this. I have a Coin System which I believe can possibly changed an implemented into yours. for my coin system I have CoinText.text = "Coins:" + Coin; Maybe you can do 2 different texts and maybe do like redText.text = "Red:" + redScore; blueText.Text = "Blue:" + blueScore;

PS: I don't know if this will help much as I too don't know much. But hopefully you found this useful.

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

77 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

Related Questions

Multiple Cars not working 1 Answer

How to make method that gets multiple types? 1 Answer

"tex.SetActive(false);" Why is this wrong? 1 Answer

Block programming for Kid 1 Answer

How to get a point that's inside a collider relative to the point a raycast hit 0 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