Skip to main content

Creating Gameplay Controller

Operating System


Now we will create the Gameplay Controller. This will be the game mechanics of the game. It will handle the flow of the game to make it playable. Create a GameObject then name it as Gameplay Controller. Then create a script and save it to Game Controllers folder as GameplayController.

Write these important variables to make the game work properly:

  1. public static GameplayController instance;
  2.  
  3. public int score;
  4. public Text scoreText;
  5. public GameObject notification;
  6.  
  7. private bool doubleBack;

Then write the rest of the codes

  1. void Awake(){
  2. CreateInstance ();
  3. }
  4.  
  5. // Use this for initialization
  6. void Start () {
  7. if(GameController.instance != null && MusicController.instance != null){
  8. if (GameController.instance.isMusicOn) {
  9. MusicController.instance.PlayGameplaySound ();
  10. } else {
  11. MusicController.instance.StopAllSound ();
  12. }
  13. }
  14. InitialGameplayVariables ();
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19. UpdateGameplayController ();
  20.  
  21. if(doubleBack == true){
  22. if(Input.GetKeyDown(KeyCode.Escape)){
  23. SceneManager.LoadScene ("Main Menu");
  24. }
  25. }
  26.  
  27. if(Input.GetKeyDown(KeyCode.Escape)){
  28. notification.SetActive (true);
  29. doubleBack = true;
  30. StartCoroutine (ShowTimer ());
  31. }
  32. }
  33.  
  34. void CreateInstance(){
  35. if(instance == null){
  36. instance = this;
  37. }
  38. }
  39.  
  40. void UpdateGameplayController(){
  41. GameController.instance.currentScore = score;
  42. scoreText.text = score.ToString ();
  43. }
  44.  
  45. void InitialGameplayVariables(){
  46. GameController.instance.currentScore = 0;
  47. score = GameController.instance.currentScore;
  48. scoreText.text = score.ToString ();
  49. }
  50.  
  51. IEnumerator ShowTimer(){
  52. yield return new WaitForSeconds (2f);
  53. doubleBack = false;
  54. notification.SetActive (false);
  55. }

After creating the script attach it to the Gameplay Controller component. And then attach all the needed components in the Inspector of GameplayController script.
tut37

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.