For anyone that wants to call the "TakeDamage" method on another object, remember that in the place of the "int d", you put the number of hearts that you want be destroyed.
I like it, i made my first thing in unity with this, only that i had to rewatch it a 100 times, bc you go really fast and there are things that i didnt notice you did bc you did it so fast that i couldnt follow, bue in the end it worked and im really happy for it, so thank you!! .I searched your game but its not longer available :(
First I create the animation I want and then in the Script, I replace the Destroy function with the animation.Play() function for the specific heart. At the end of the animation you can disable or Destroy the gameobject
@@gamedevel22 I can't do that. I Got this error: "The animation state heart could not be played because it couldn't be found! Please attach an animation clip with the name 'heart' or call this function only for existing animations."
@@gamedevel22 Thanks. Casually i solved that after i posted that question. For anyone struggling with TakeDamage, is important to clarify the amount of damage the Player is going to receive.
@@gamedevel22 ua-cam.com/video/5TB2ptFfdkQ/v-deo.html took the time to do so, and added it directly to player script with a few effects but more to come :) thanks for your Tutorials man :)
@@gamedevel22 Hey man, using the destroy function I'm finding it hard to allow hp potions (as randomly generated power ups) any ideas, Instantiate(hearts[life].gameObject); throws index outta range exceptions
Hey, you could just disable and enable the gameobject when you get a potion. Store all Hearts is one Array and use hearts[i].gameobject.setactive = false or true
Hi, after pressing the button only one heart disappears, I can still hit the button but nothing happens. Tried re-adding the duplicated heat image but still doesn't work? Any help appreciated, thanks
Hey, very good Tutorial thank you. But if I´m trying to call the TakeDamage Funktion on a "OnTriggerEnter2D" Function then it doesnt work. The life variable will be set from 3 to 2 like it should(I have tested it), but somehow the if statement in the Update function doesn´t recognize the change of the life variable... Do you have an Idea why ? PS. : when I use a button to call the takeDMG function like in the tutorial it works perfect.
Hey thank you! Are you sure you are using the correct variable in the if statement of the update function? The way you call the take damage function should not be a problem even with on Trigger enter.
@@gamedevel22 thanks for the fast answer i found the solution, i had another script that deleted the playerobject and instantiated a new playerobject on a trigger enter at a respawnpoint. That caused the integer to reset to 3 each time he got hit🤦🏼♂️
Hello! Thank you for your tutorial! Really helped. I have a question though, What could you do if you used this in a game with levels. I would use DontDestroyonLoad but if you do use that it would cause your main character to duplicate.
how can I acces void TakeDmg(in my case Takeshoot), use it as a code by other words...for example I have glob float, that checks if my player(ball) is moving, if its moving it should loose one life. I guess I should use if aswell..if (golbvalue.playerismoving){ takeshot();}...but it cant go that way..
@@bayakFPS Glad you solved the problem! I suppose it destroyes all the hearts because the condtions of the if stameent stay always true. You can adda new boolean that is true at the beginning and add it to your if conditions. in the first line inside the if statement turn the bool to fakse. You just need to think about where you need to change it back to true in order to lose a new life later on!
@@gamedevel22 I've been trying for a while to get ontriggerenter2d to decrease my health I believe my script is wrong between the {} of the ontriggerenter using System.Collections; using System.Collections.Generic; using UnityEngine; public class HeartSystem : MonoBehaviour { public GameObject[] hearts; public GameObject Player; private int life; private bool dead; private void Start() { life = hearts.Length; } void Update() { if (dead == true) { Destroy(gameObject); } } private void OnTriggerEnter2D(Collider2D other) { TakeDamage; } public void TakeDamage(int d) { if (life >= 1) { life -= d; Destroy(hearts[life].gameObject); if(life < 1) { dead = true; } } } }
Well at the beginning of your scene you will always have full life since this is how it's set up. Your starting life will only be affected if you save your current life in a variable that you read when starting your game over.
For anyone that wants to call the "TakeDamage" method on another object, remember that in the place of the "int d", you put the number of hearts that you want be destroyed.
i dont get it
@@MikeCedillos You define the "d" value : want to take 2 damage? d=2; 3? d=3 etc.
Wicked, I needed the player to collide with a lava object to take away a life. Thanks for saving my time with this comment!
This was a great tutorial that gets straight to the point, subbed!
Great to have you on board 😁
leaving a comment so it gets shown to more people
I like it, i made my first thing in unity with this, only that i had to rewatch it a 100 times, bc you go really fast and there are things that i didnt notice you did bc you did it so fast that i couldnt follow, bue in the end it worked and im really happy for it, so thank you!! .I searched your game but its not longer available :(
Lol I thought this was gonna be something spiritual 🤣 looks like a good tutorial, I’ll save to watch later. 👍
Haha definitely Spiritual 🤩
Thank You!! Working very well...
This helped me a lot, ThankU!
Where is the "increase life" part as it says in the video comments?
how do you make it play the destroy animation
First I create the animation I want and then in the Script, I replace the Destroy function with the animation.Play() function for the specific heart. At the end of the animation you can disable or Destroy the gameobject
@@gamedevel22 I can't do that. I Got this error:
"The animation state heart could not be played because it couldn't be found!
Please attach an animation clip with the name 'heart' or call this function only for existing animations."
This means that you dont have an animation component on your gameobject! Assign it and then choose the animation you want to play
Awesome tutorial.
10/10, thanks.
Awesome thank you
I applied this to my project thanks to you!
i want the lifes to go away on trigger man
You can call any function you like from within OnTriggerEnter
This is great explanation video. Thanks ! i was wondering if there is a way in this system to implement 'add life' as well as 'take damage' ?
Thank you! Yes it is possible for sure..check out one of my latest Videos about life system and potions! I think it will help you out a lot 😄
@@gamedevel22 GREAT !!!! thanks for pointing this out, i found the potion video, will watch now. :)
I cant find "Size" and whenever I typed "GameObject" it appeared white instead of green
im having a problem with this like my first heart gets destroyed but the other 2 stay on screen
Are you sure that your Script is like the one in the tutorial?
how do I move the heart image? mine is just stuck in the middle
Is the Image assigned to a parent gameobject? Try moving that one around or play around with the anchor points
Anyone else getting to 2:53 and it comes up "List is empty" under hearts?
Edit: Wait nvm, fixed it
how can i lose life when i fall off the platform
Overall it works perfect. I just have one question... how do you use the TakeDamage method in another game object?
Do you mean how to call it? The object that will collide with the Main character could call the function inside the onTriggerEnter function
@@gamedevel22 Thanks. Casually i solved that after i posted that question. For anyone struggling with TakeDamage, is important to clarify the amount of damage the Player is going to receive.
@@gamedevel22how do you call it like what's the code
Thanks a lot but now how do I make my player lose a heart when he touches an enemy ???
You can call the same function inside of OnTriggerEnter whenever you touch an enemy
Thanks as always savvu
Hope to see this System in one of your games!
@@gamedevel22 ua-cam.com/video/5TB2ptFfdkQ/v-deo.html took the time to do so, and added it directly to player script with a few effects but more to come :) thanks for your Tutorials man :)
@@gamedevel22 Hey man, using the destroy function I'm finding it hard to allow hp potions (as randomly generated power ups) any ideas, Instantiate(hearts[life].gameObject); throws index outta range exceptions
Hey, you could just disable and enable the gameobject when you get a potion. Store all Hearts is one Array and use hearts[i].gameobject.setactive = false or true
@@gamedevel22 I was starting to think that(used something similar to create a wpn swap system) thanks for the advice and the line of code lol
5:32 int d on this method error because it never in used.
Hi, after pressing the button only one heart disappears, I can still hit the button but nothing happens. Tried re-adding the duplicated heat image but still doesn't work? Any help appreciated, thanks
Hey I may need to check your Code to find the Problem..feel free to text me on Discord 😁
I am having a error:
There is no argument given that corresponds to the required formal parameter 'd' of 'GameManager.TakeDamage(int)'
In the function Parameter you will also need a name for your int variable. So you could write (int d)
thank you, my problem is solved
Awesome nice to hear that!
Hi, can you show me how to make random obstacle generation?
Your way with platforms works great, Thank you !
Of course! It will be on my to do list for next week
Hey, very good Tutorial thank you. But if I´m trying to call the TakeDamage Funktion on a "OnTriggerEnter2D" Function then it doesnt work. The life variable will be set from 3 to 2 like it should(I have tested it), but somehow the if statement in the Update function doesn´t recognize the change of the life variable... Do you have an Idea why ?
PS. : when I use a button to call the takeDMG function like in the tutorial it works perfect.
Hey thank you! Are you sure you are using the correct variable in the if statement of the update function? The way you call the take damage function should not be a problem even with on Trigger enter.
@@gamedevel22 thanks for the fast answer i found the solution, i had another script that deleted the playerobject and instantiated a new playerobject on a trigger enter at a respawnpoint. That caused the integer to reset to 3 each time he got hit🤦🏼♂️
Glad you found the solution! Keep it up :D
Hello! Thank you for your tutorial! Really helped. I have a question though, What could you do if you used this in a game with levels. I would use DontDestroyonLoad but if you do use that it would cause your main character to duplicate.
Yes, it would cause your character to duplicate so you have to write a script that destroys the other duplicates.
i get as a error that the assetbundle name hasnt been set
That's strange..maybe it has something to do with your unity project or some assets you Imported?
how can I acces void TakeDmg(in my case Takeshoot), use it as a code by other words...for example I have glob float, that checks if my player(ball) is moving, if its moving it should loose one life. I guess I should use if aswell..if (golbvalue.playerismoving){ takeshot();}...but it cant go that way..
I managed to do it, but instead of just one heart, it destroys all heart..code is same as yours. Any idea?
And I use no1 in ()..
@@bayakFPS Glad you solved the problem! I suppose it destroyes all the hearts because the condtions of the if stameent stay always true. You can adda new boolean that is true at the beginning and add it to your if conditions. in the first line inside the if statement turn the bool to fakse. You just need to think about where you need to change it back to true in order to lose a new life later on!
@@gamedevel22 thanks bro, works now!
keep up w good work!
What do I do if the player loses a life when it comes in contact with a spike or enemy?
You can use OnTriggerEnter and use the same Code as in the video
@@gamedevel22 where do i put the ontriggerenter
I would put it in the players script and check there if he touches the spike for example by giving the right tag to the spike.
Is there a way to make it so if it touches something the health decreases like spikes or an enemy
Yes of course! Just have an onTriggerEnter function and whenever you touch something that causes damage call the function of the video
@@gamedevel22 I've been trying for a while to get ontriggerenter2d to decrease my health I believe my script is wrong between the {} of the ontriggerenter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeartSystem : MonoBehaviour
{
public GameObject[] hearts;
public GameObject Player;
private int life;
private bool dead;
private void Start()
{
life = hearts.Length;
}
void Update()
{
if (dead == true)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
TakeDamage;
}
public void TakeDamage(int d)
{
if (life >= 1)
{
life -= d;
Destroy(hearts[life].gameObject);
if(life < 1)
{
dead = true;
}
}
}
}
You need to call it like this..TakeDamage(1); The 1 is just an example and you can use any number to reduce your health based on that
@@gamedevel22 okay thanks im not too good at this kinda stuff you helped me alot
Hey!
If I press restart button in the game, how to restart my lifes?
Thank you!
Do you mean how to start with full lifes or how to continue with the amount of life from before?
@@gamedevel22
How to start with full lifes, exactly this :D
Well at the beginning of your scene you will always have full life since this is how it's set up. Your starting life will only be affected if you save your current life in a variable that you read when starting your game over.
I followed all the steps but I can't click the button? Please help
Have you tried creating a new ui button and assign to it the function to be called?
@@gamedevel22 Yes, I followed the steps but the button is just unclickable, and hovering over it doesn't make it darker either
Maybe some other ui object is over it and Blocks the touch
good tutorial but music annoying
it dosn't work don't ewaste your time.
Horrible tutorial doesn't even cover how to add health, or take damage when a player is hit.