Skip to content

Commit

Permalink
Updates project1-enhancements/playercontroller-v2.md
Browse files Browse the repository at this point in the history
Auto commit by GitBook Editor
  • Loading branch information
kdoore committed Feb 12, 2020
1 parent a4c967f commit 264effd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion gamedata.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
### GameData - Version 1
## GameData - Version 1


In the game we'll need an object that is persisted throughout the gameplay session so that we can keep track of score, lives, health, etc.

Below is a simple class definition for GameData, we'll add more to this as we progress.

###Singleton Pattern

This script will be attached to the GameManager in the first game Scene and will be persisted throughout the game since it uses the singleton design pattern.

The Singleton Pattern accomplishes 2 things:

1. Insures only **one instance of an object exists** at anytime during the application's lifetime. Any duplicate objects are destroyed in the Awake( ) method.

2. A **static variable provides global access** to the Singleton object instance.

###GameManager Singleton GameObject
**Add GameData script to an Empty GameObject:** named: **GameManager** in your starting scene, so it will be executed as a singleton and shows up as 'DontDestroyOnLoad' in the Hierarchy when the game is played.

**DontDestroyOnLoad - in Play-mode**
Expand Down
4 changes: 2 additions & 2 deletions project1-enhancements/playercontroller-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public class PlayerController : MonoBehaviour {
{
Debug.Log("Hit Collectible");
PickUp item = hitObject.GetComponent<PickUp>();
GameData.instanceRef.Add(item.value);
GameData.instanceRef.Add(item.Value);
Destroy(hitObject.gameObject);
}
else if (hitObject.CompareTag("Hazard"))
{
Debug.Log("Hit Hazard");
PickUp item = hitObject.GetComponent<PickUp>();
GameData.instanceRef.TakeDamage(item.value);
GameData.instanceRef.TakeDamage(item.Value);
Destroy(hitObject.gameObject);
}
else
Expand Down

0 comments on commit 264effd

Please sign in to comment.