day 15

Oksana Vasquez
2 min readDec 22, 2020

Player Lives And Damaging Reboot

Today I learned how to do damage to the player. We used a concept of Life for that and, of course, it should be store in variable. So first we will create a variable for that. To damage a player we create public method and we call it Damage, we make it public so other scripts can see it.

And this method we created for damage needs to be called from other script… have direct access to like in other.transform. So we type:

other.transform.GetComponent<Player>().Damage();

And that means we call “other” which in this case is object Player, then we call transform of the player and from there we go to script Player which has method Damage.

I also learned that it’s important to check if the object I refer to exists so there no errors in the game. And we do so using if statement.

if (player != null)

{

player.Damage();

}

Im learning about Spawn Manager which we use to create continuous flow of enemies or other objects. The Spawn Manager is not a physical object we can see but it has certain behavior we need. We create Empty Object in Hierarchy for this.

I solved Challenge SpawnRoutine Reboot.

Let’s say I want to spawn object every 5 seconds. We need to use coroutine for that. We can check in google how to use Coroutines. A coroutine is like a function that has the ability to pause execution and return control to Unity but then to continue where it left off on the following frame. In C#, a coroutine is declared like this:

I Enumerator MethodName()

I Enumerator is type of method which (unlike void type) let us use yield and yield let us to tell Unity that we want to wait certain number of seconds as a coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes.

To solve this problem I also need to use while loop:

while(){

--

--