Video 47. “Player L...

Video 47. “Player Lives and Damage”  





Ultimate Guide to Game Development with Unity 2019

Video 47. “Player Lives and Damage”

~9:50 discussion of Null Check component


Code example:


if (otherObject.tag == "Player"

If Player player = other.transform.GetComponent<Player>(); 


If I understand this correctly, there are four instances of the term PLAYER: as variable, as game object, as component, and as tag.  This is really confusing when trying to understand the functions of each use.

The part  (If Player player =…) is not explained well and I can’t understand what’s going on here. 

The instructor says, “We can simply create a variable of type player called 'lower case player'…”. I thought variable types were FLOAT, BOOL, INT, DOUBLE, CHAR, or STRING.  So where did this PLAYER type variable come from?

Are we in essence creating a new variable FOR the Player class OUTSIDE of the Player script? (we are editing the Enemy script at this point)

If so, how does Unity know what variable type (INT, FLOAT, etc) since that's normally set at the beginning of the new variable declaration but not present in this case?


Thank you!

1 Answer

So in C# everything is essentially an Object, as every "type" inherits from that.. So actual string, int and such are built-in classes, so the compiler (C#) knows about these no matter what.  Now in this case he is making a variable player (lowercase) from Object Player (uppercase P).  This comes from the class Player.  

You get that from doing this:

public class Player : MonoBehaviour

This creates the type Player, that inherits all the properties and functions from MonoBehaviour.  So in essence you are CREATING a new type to use.




Scroll to Top

Please Login or Register