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 SecretAgentMango · Jul 28, 2017 at 12:10 AM · scripting problemscore systemcoins

Scripts for counting score not working?

Hey guys, novice here. I'm having a problem with some of my scripts that are supposed to count score for the player as they collect coins. I have two C# scripts. The first one is CoinCounter.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 [ExecuteInEditMode]
 
 public class CoinCounter : MonoBehaviour 
 {
     public int coinCount = 0;
     
     // Update is called once per frame
     void Update () 
     {
         GetComponent<Text>().text = "x" + coinCount;
     }
 }

So in this script (above), I'm updating the UI Text (also the script is attached to the UI text). The UI text will "increase in value" whenever the public int coinCount goes up in number. I didn't have any issues with this script, but I did with the other script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Coin : MonoBehaviour 
 {
 
     private CoinCounter coinCounter
     // Use this for initialization
 
     void Awake ()
     {
         coinCounter = GameObject.Find ("coinText").GetComponent<CoinCounter>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.tag == "Player") 
         {
             coinCounter.coinCount++;
             gameObject.SetActive (false);
         }
     }
 }

This script is attached to my coins in the scene. This all makes sense to me, but for some reason I'm getting a couple errors:

  1. I have an error attached to my void Awake ( ) that says: "Assets/Scripts/Coin.cs(11,5): error CS1519: Unexpected symbol `void' in class, struct, or interface member declaration". I have no idea really what's wrong with my code here.

  2. MonoDevelop is telling me that "The name 'coinCounter' does not exist in the current context". Am I creating a private variable incorrectly? I thought this was the proper way to grab a variable from another script...

Thanks to anyone who can 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

3 Replies

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

Answer by RalphWeis · Jul 28, 2017 at 12:36 AM

at the second script line 8 you forgot semicolon

  private CoinCounter coinCounter;

instead of

  private CoinCounter coinCounter
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 SecretAgentMango · Jul 28, 2017 at 05:18 AM 0
Share

. . . I hate myself right now, but I love you! Thanks :)

avatar image
0

Answer by UnityCoach · Jul 28, 2017 at 06:03 PM

If I may, in your first script, don't use GetComponent on every frame! Make a reference to it at least.

 public class CoinCounter : MonoBehaviour 
  {
      public int coinCount = 0;
      Text textComponent;
      void Awake () 
      {
          textComponent  = GetComponent<Text>();
      }
 
      void Update () 
      {
          textComponent.text = "x" + coinCount;
      }
  }

Now, better if you make that a property :

 public class CoinCounter : MonoBehaviour 
   {
       private int _coinCount = 0;
       Text textComponent;
 
       void Awake () 
       {
           textComponent  = GetComponent<Text>();
       }
  
       public int coinCount
       {
           set
           {
               _coinCount = value;
               textComponent.text = "x" + _coinCount;
           }
       }
   }

Changing the property will update the variable, and update the display only once instead of every frame.

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
avatar image
0

Answer by SaiTarun · Aug 18, 2017 at 07:44 AM

public void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "collection") {

         Destroy (collision.gameObject);
         currentscore = currentscore + 1;
         score (currentscore);
         sourcesound.Play ();

     }
 
 }    void score(int score)
     {
     Debug.Log(score);
     txt.text="score:"+ score;
     if(score==6)
     {
         txt.text="***********winner*********" ;
             
     }

     }
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

112 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Local Party Game Score Transfer 1 Answer

How to give score only once for each level? 0 Answers

How to increment score by one, every time player moves the device face down ? 2 Answers

how to give star score once per each level? 0 Answers

How to add different score when player hit different enemy,how to add different scores while player hitting different type of enemies 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