- Home /
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:
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.
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!
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
. . . I hate myself right now, but I love you! Thanks :)
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.
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*********" ;
}
}