I’m making a simple game where you click a bunch of blocks trying to find a specific one, and I want to increase the score with every block you click. Right now I have it so that OnMouseDown the block will disappear and increase the static score by 1. What I’m having a hard time doing, is connecting the blockScript (which has all of my OnMouseDown functions) to my Text UI “Score”. I thought about having a second script called scoreManager to simply call the function to display the increased score, but I couldn’t find a way to do it.
But I must say that having the score static is kinda dangerous, because anything else could change the value, though for a small game I think it is legitimately okay to use a static.
If you’re going to make anything static, either use a Singleton or only expose the static Getter of a property, not a field (likely both).
You need to be able to either stop changes from being made directly elsewhere, or at least validate the changes using static properties and functions rather than fields. I agree that “Score” in a small game has a perfectly valid reason for being static (there will only ever be one of them), but in that case it would be far better to make a Singleton “GameManager” that holds all values that, like Score, can only exist once and need to be accessed from several places.
Even better would be to build or use an event messaging system so that the UI gets notified of a change without having to check every frame.
what would be a way to do it other than making it static? and honestly I don’t even know what most of the stuff you guys are talking about are haha I’m fairly new to coding.
I’m honestly not sure how I’m supposed to use those Singletons. The way I’m trying to do it now is just update the text component of my canvas with the current score.
Something like going
public static int gameScore ;
public Text Score;
void Start () {
gameScore = 0;
Score = "Score: 0";
}
void OnMouseDown () {
gameObject.SetActive (false);
IncreaseScore ();
}
public void IncreaseScore (){
gameScore += 1;
Debug.Log (gameScore);
Score.text = "Score: " + gameScore.ToString();
}
}
But the problem I’m running into is:
Assets/_Scripts/Scene 01/blockScript.cs(7,16): error CS0246: The type or namespace name `Text’ could not be found. Are you missing a using directive or an assembly reference?
I’ve tried replacing It with GUIText but the results are :
Assets/_Scripts/Scene 01/blockScript.cs(13,27): error CS1061: Type int' does not contain a definition for Text’ and no extension method Text' of type int’ could be found (are you missing a using directive or an assembly reference?)
Assets/_Scripts/Scene 01/blockScript.cs(28,17): error CS0103: The name `Score’ does not exist in the current context
First, it looks like you’re using Notepad or something to code. Don’t… You should use a proper tool like Visual Studio Community (Free) Version, or MonoDevelop (included with Unity). It helps a lot with autocompletion, suggesting fixes and telling you when something doesn’t work.
If you’re working with UI then you have to include it in your script. The top part of your script should be something like:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
Now your script knows what a Text is because its “using UnityEngine.UI”.
However, Text is a component and not a string, so you can’t just say Score = "some string i want"; because you’re trying to shove a string into a component and Unity thinks you’re insane and says no.
So to do that you need to find a real string in the Text component. This is Score.Text, thats the string.
In general, you’re missing a lot of basic ideas and should probably go over syntax fundamentals and the unity tutorials in the Learn section of the site.
First of all, I’m using Unity’s Mono Develop. However, this particular piece of code I did write in the comments section you Caught me. lol
That being said, when I opened mono I noticed that most of the things you pointed out I already had in my code. Everything except UnityEngine.UI. I thought unity automatically included that, and it never dawned on me to even check. (currently beating myself up about that lol.)
Now that I have added that, everything seems to actually be working (I hate it when its something stupid like that.)
Now that it works, how can I add the score text as the “score” value on my script without having to drag and drop all of them.