Day 13 & 14

Frankie Rodriguez
6 min readNov 28, 2020

--

Warning! This is an excessively long blog but for good reason. Medium would not allow me to upload yesterday’s blog. Today I worked on my game. The development so far has been a long arduous process, but it is beginning to take shape.

Lives Display: By defau5lt I will begin with 3 lives. To start I right clicked on canvas -> add image component -> UI -> image. I titled lives_display_img -> dragged 3 lives sprite to source image -> anchor image -> set position -> preserve aspect ratio. When done, I opened up Visual Studios, here I will code to add 2 lives, 1 life, no life remaining. To do so I am going to maintain a reference to the sprite I am working with. Instead of a variable for each of those sprites, I created an array — private Sprite[] _liveSprites; then I will serializeField to see code activated in inspector. Back on Unity, when I click on canvas, a sprite drop down exist. Next I set how many textures I have, in my case it will be 4. I then associated each ‘element _’ with how many lives I need by dragging each sprite into the following — element 3 = 3 lives, element 2 = 2 lives, element 1 = 1 life, element 0 = no lives. Back to uiManager on Visual Studios, I needed to be able to access the actual image. To do so, I used the code private Image _LivesImg; -> serializedField to assign in inspector. Back to unity, I clicked on canvas, referenced lives img by dragging image from hierarchy. Back to visual studios, I go the section where I coded for score updates. Here I updated lives by using the same code format used to update score — public void UpdateLives(int currentLives). To access the lives, I need to access the display img sprite and give it a new one based on the currentLives index. To do so I coded it this way.

To call this update, I go to the Player C# script, to update lives. Access _uiManager.UpdateLive(_lives); Once I completed the code, I went back to Unity to test. I then clicked on Lives_Display_Img to ensure the source img is 3 lives since this is the number of lives I want to start with.

Game Over: Next, I worked on developing the game over text object and flicker effect. First, I created the text by right clicking on canvas -> select text title text: game_Over_text. As I do not want it to appear in the beginning I will uncheck the box next to the title. Next, I want the uiManager C# Script to know it exist -> Visual Studios, -> create a variable to store image — private text _gameOverText; serializeField to view in inspector. Back on Unity, on the canvas, I now have variable to apply text too. I dragged and dropped the game_Over_text to the uiManager script which allowed me to control that text object. Go back to Visual Studios, on the start screen when the score is set to 0, I made sure the gameover text is off by coding _gameOverText.gameObject.SetActive(false_); <-this will turn off that object at start. So, when do I turn on gameover text? A: when lives are updated. Under that code, I will turn the gameover text on by using and if() statement. I will also add a special effect flicker on the game over text by creating Coroutine and while loop.

Restart Level: To start, I created text on canvas, process is similar to gameover text, I titled Restart_text. As with game over, I do not want this game object to appear in the beginning, so I will uncheck the box next to the title of this text object and go to Visual Studios to make uiManager aware of this gameobject. To do so I used the code private Text _restartText; serializeField to view in inspector. I went back to Unity, dragged Restart_text to uiManager Script. Returning to Visual Studios, I started a new method, copy and paste -> void game over sequence () paste gameOverText and flicker code into this method. So if (currentLives == 0) I am going to call in GameOverSequence(); this way we have a dedicated method to debug problems related to the gameOver text object.

To restart the game, I created a Game_Manager C# script, which is responsible for managing the state of the game i.e. is the game over, is the player alive, how many enemies are there etc.. <- this can be used as a state of the current state of the game and is a good place to provide logic. In the hierarchy ->create empty object -> title game_manager. Go to the project section -> scripts ->right click -> create -> C# script -> title GameManager -> drag and drop script to the Game_Manager object. Once done, I double clicked on the script to open Visual Studios, where I cleared void start and update -> in manager class, create private bool _isGameOver; — by default this is false. I am also going to [serializeField] to view in inspector. To turn bool statement true, I am going to call in Game Over method i.e. public void Game () { _isgameover = true; }

To develop user functionality, I created private void Update () method for the user input so if the r key was pressed the current scene will restart. Next, I searched online, via Unity Documentation, looked up SceneManager.LoadScene. Here is the link:

Unity — Scripting API: SceneManagement.SceneManager.LoadScene (unity3d.com)

So using what I learned from Unity, under the GameManager C# Script, I first created a sceneManagement namespace — using UnityEngine.SceneManagement; — this will include the proper libraries to operate and adjust the scene settings. Then I created an if() statements (reference lines 13–16).

To translate if (player presses down (R) && “and” _isGameOver true) restart the scene. To know what scene to use, I go back to Unity, go to the scene prefab, locate the name of the scene, in my case “Player”. An error will occur when you try to run this code, why? The scene has not been added into the bool setting. To do so file -> build settings -> add open scenes -> choose scene. I noticed that the index value is 1, so I go back to Visual Studios and instead of LoadScene(“Player”) I can replace it with 1, which will be more efficient in loading a scene rather than by a string i.e. “Player”; strings a a bit slower to load. To help identify the current scene I write a pseudo code //current game scene after the code. Next, I need to call the _isGameOver = true. To do so I went to the uiManager, created a reference handle private GameManager _gameManager. In void start I assigned the handle to the component _gameManager = GameObject.Find (“Game Manager”). GetCompenent<GameManager>(); -> go down to the game over sequence and accessed the gamemanager.GameOver(); Always null check. Here is the complete code sequence:

And just when you think, “hey look at that I’m 70% complete” — Jonathan starts to say “this is only the beginning”…………

Help!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Frankie Rodriguez
Frankie Rodriguez

No responses yet

Write a response