SCRIPT COMMUNICATION
OBJECTIVE: Access the method DisplayName()
within the GameManager Class through the player class
GAMEMANAGER SCRIPT
CODE BREAKDOWN
LINE 3 --> Creates a private static instance of the current class
_instance
is going to define the instance of THIS GameManager and there never can be more than ONE instance of a single class
LINE 4 --> Creates a public static instance of this class
Needed for script communication
Needs to be a property that other classes GET but cannot SET
LINE 6-12 --> Defines the GET property for the Instance property
when another script calls upon the Instance property the following happens...
FIRST: program checks that _instance is not NULL (I.E. that the class exists within the hierarchy)
SECOND: If not NULL the property returns the instance of itself, allowing the other script access
LINE 15-18 --> Initialized the singleton while the program initially loads
This is where the program assigns _instance to this script
LINE 20-23 --> Method the player class will access displaying "My name is Jordan." within the Unity Console
PLAYER SCRIPT
CODE BREAKDOWN
LINE 5 --> Function that calls upon the GameManager singleton and calls the method within a single line of code
SINGLETON CLASSES SHOULD BE COMMUNICATED WITH AND SHOULD NEVER COMMUNICATE WITH NON-SINGLETON CLASSES --> Singleton Classes can communicate with each other ==> GameManager Class can communicate with UI Manager Class
Last updated