Search code examples
c#unity-game-engineflappy-bird-clone

Public variable name in script does not match field display in inspector/editor


S0 I am following a Unity tutorial on YouTube. It is a flappy bird clone. Everything is going good but I ran into a piece of code that I just can't wrap my head around. I have attached to images ( one of the unity inspector and the other is the script).

My question is that in line 8 of the GameControl script, a variable gameOverText is created of the type GameObject. Then on line 27 the gameOverText.SetActive("true") is called. I don't see how unity ties the gameOverText variable to the GameOverText in the unity inspector.
I know I dragged the GameOverText from the Hierarchy to the Game Control. As you can see in the pic the GameOverText in the inspector is spelled differently than the "gameOverVariable" in the script.

I just do not see how it works.

enter image description here enter image description here


Solution

  • Both public GameObject gameOverText and the field "Game Over Text" displayed in the Unity inspector refer to the same variable. When Unity displays variables in the inspector, it splits up the variable name by capital letters and then capitalizes the first letter of the first word if needed. (Presumably this is to make variables more human-readable.)

    In this way, we can see that "gameOverText" => "Game Over Text"; when you drag a reference onto the "Game Over Text" field, you are in fact setting the value of the gameOverText variable.