I don’t see anything “wrong” in your code in the sense that I don’t see any “error”.
However, there are a couple of things that are important to know from the start in Unity, in my opinion.
In general it’s best to avoid trying to retrieve gameobjects and/or components at runtime (while the game is running) because it has a heavy cost in performance. For example, this line here: bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<BirdScript>();
.
What you should do instead, if your bird
GameObject is already in the scene from the start, is to add to your LogicScript
a variable public BirdScript Bird;
, this variable is public so it is serialized by Unity, meaning that if a GameObject in your scene has a LogicScript
attached to it, Unity will “serialize” meaning “save” its serializable variables and remember them.
So if you select your LogicScript GameObject in the hierarchy, you should see your Bird
variable in the inspector window. You can then drag and drop your “Bird” GameObject from the hierarchy onto the variable in the inspector, and then save your scene.
Now you don’t even need the line bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<BirdScript>();
because Bird
will already have a reference to the BirdScript
you are looking for.
You can also do this for your high/low score TMP_Text
s if they are already in the scene.
You can also do this within prefabs.
What you can also do to really know what is going wrong is to look into the logs of your program, although I don’t know how to access them on android, there is probably a way to get them out, because if you have for example a string
that is null
(which is not the same as an empty string), you might get errors when you are doing int.Parse(scoreTextString)
for example. For that reason it’s often better to use TryParse
instead of Parse
unless you are absolutely sure that the text you’re trying to parse is, for sure, a valid number.
If you look at the logs, I would expect you to see something like “NullReferenceExeption” followed by the line and type in which it happened, so you can find the line of code where things went wrong. Here for example, if GameObject.FindGameObjectWithTag("Bird")
does not find any GameObject, it would return null
, and then the following .GetComponent<BirdScript>()
would throw an exception because it would try to access … something that doesn’t exist, and the code that comes after an exception is skipped, so LoadHighScore();
would never be executed in my example.
A side note but I would have liked to know that when I started unity, remember that GetComponent
is heavy because it has to access the C++ side of unity and that has a huge cost, so try to store your components in variables instead of getting components over and over each frame.
One last thing, when it comes to UI, it should only be used to show data, it should not hold any data. So it would be better to store your scores as int
variables, and work with that, instead of trying to get your score data out of the string displayed to the user. If you want to go much deeper into how to write code I suggest checking out the S.O.L.I.D. principles and just try to get a very basic understanding of what they stand for and why, it’s far beyond your concern here, but I also would have liked to hear about it much sooner when I started Unity.
Also, in a few months when you wonder how to really organize your code well, take a look at VContainer, I can’t imagine starting a project without something like it nowadays.