Coin Collection Counter
I am a moderate C# coder but don't have much experience with UI. I received the following code from a friend but am facing some bugs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class collection : MonoBehaviour {
public Text countText;
public int coinNum;
void Start () {
coinNum = 0;
countText.text = "Count: " + coinNum.ToString ();
}
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "bonecoin") {
other.gameObject.SetActive (false);
coinNum++;
countText.text = "Count: " + coinNum.ToString ();
}
}
void Update () {
countText.text = "Count: " + coinNum;
}
}
I am able to see the counting total in the Inspector window when I select the player, but the text is not updating each frame. Also, when I exit play mode, the text updates.
For example, if I enter play mode and collect 5 coins, I won't see the text say "Count: 5" like it should. However, once I exit play mode, I see the text read "Count: 5". The text continues to stay that way until I enter and exit play mode again. The text doesn't even change when I enter play mode and start the game.
Before I posted this, I looked up many things to try and find a way to fix my problem including some other great questions posted on this site, but none of them helped.
Answer by ricke44654 · May 24, 2016 at 05:17 PM
Hmm, this jiggles a thought as I just went through the 2D UFO Game tutorial a day or two ago. Is your counting text a child of a Canvas element in the hierarchy? I seem to recall some mention of not being able to see the text render on the screen if it's not in a Canvas element. When you add a UI Text element to the hierarchy, it creates new Canvas and EventSystem objects if they didn't exist before and places the new Text element under the Canvas.
One other small note - it would probably make sense to put the text update in a function that you can call from multiple places to eliminate duplicate code. I noticed your code in Update wasn't converting coinNum to a string, while the other places were converting it. :)
Also try changing the coinNum to Private - it's possible you've entered a number in the public box in the inspector which is overriding the count.
Hey, thanks for the response @ricke44654. I did what you suggested and I will try to use that technique more often. Sadly, this didn't change anything.
By the way, the counting text is a child of a Canvas with EventSystem object present. I also have the counting text itself as a prefab since I couldn't find a way to access it any other way.
@$$anonymous$$mmpies thank you too, but nothing changed sadly.
Ah... perhaps you have just given a clue to the issue. You say you have your counting text as a prefab, and also a counting text instance in your hierarchy, correct? I just tried a little test on my UFO Game to setup what I think you might have:
I added a prefab of my counting text to the project (I only had an instance in the hierarchy before).
I have a public property in my script, as you do, for my counting text. It contained a reference to my counting text instance from the hierarchy.
I removed that reference in the Inspector (highlighted the property on the script in the Inspector and pressed the Delete key), then replaced it by dragging the prefab instance into my script on the Inspector. Surprisingly, the icon looks exactly the same for the text element. $$anonymous$$ore on that in a moment.
I ran my game and guess what? $$anonymous$$y score text never updated. Nifty.
I removed the prefab instance from my script and replaced it with the instance from the hierarchy. Boom - score updates just fine.
All that to say -- I think you may have dragged your prefab instance on to the script property while having an instance in the hierarchy as well. Internally, Unity could be updating that prefab reference ins$$anonymous$$d of the instance on screen... in essence, you'd have two separate elements with no relation between each other.
Normally, from what I've seen, you usually have a prefab icon (little blue cube) beside a reference when you use a prefab... that's why I was surprised the icon was the same in the Inspector. Give this a try and see if it fixes your issue.
Thank you so much! That is the breakthrough!
Sadly, there is one problem. When I had the original text object (not a prefab yet), I was unable to drag it into the instance on the script. After that didn't work, I made it a prefab and was able to drag the instance from my prefabs folder into the script instance. For some reason, I cannot have the instance in the hierarchy enter the instance in the script like you said it should.
I don't know why you are able to make the hierarchy instance update and I am only able to have the prefab instance update. This is almost the same concept of my friend (the same one who I got the original code from) able to make it work but not me.
Hmm... that is certainly odd. Your public variable in the script is a Text type, so it should accept any Text type element you drag on to it, prefab or not. Couple more thoughts:
Have you verified that your count text in the hierarchy is a Text element and not some other object type? As far as I know, the types have to match in order for the Inspector to let it be used.
Try adding a new Text element to your hierarchy and see if you can drag it into the CountText reference on the script in the Inspector. If that works, kill the old count text object and be happy. :)
If none of that works, I'd say something else is wrong, but I don't know what it would be. :/
Thank you so much @ricke44654 for all your insight but sadly none of it works.
@morstix - Rats. $$anonymous$$y suggestion would be to create a little test project, try adding only a Text element to it (no prefab), then update the value in a script with a public variable on it that you drag to the Text element on to it. If that doesn't work, then it seems like you have some kind of Unity bug / issue. Otherwise, if it works, then it's got to be something in your project. I could take a look at your project if you wanted to zip it up somewhere, but that's up to you. :)
@ricke44654 : UPDATE: I created another project that needed a collection counter. It worked! I think my problem was I imported the project I was previously working on and some weirdness happened. Just wanted to let you know it worked so other people can use this properly. Thank you so much.
Answer by sundhar_unity · Aug 19, 2020 at 05:07 PM
Hope this will help: https://www.youtube.com/watch?v=GgNLr7SSh1I&t=107s
Your answer
Follow this Question
Related Questions
How can I change the UI button color when clicked 1 Answer
What is the SetDirty and SetDirtyCaching in Unity UI 0 Answers
ScrollRect content/Vertical Layout Group content resets to middle when children altered 1 Answer
What do you insert in the "" area in this timer? (00:00) 1 Answer
UI elements not visible on iPhone 5s 0 Answers