Category Unity 2d player health script

Unity 2d player health script

Hi I have made a simple health script, using Unity's GUI to create a health bar which decreases as you loose health. I've also added a button which makes you loose 10 health each time you press it.

Now I'm trying to script so that your current health stays the way it was last scene, when you change scene.

I have tried using PlayerPrefs, and it does work since the current health is the same when you change scene. Though if you quit the game and re-enter you still have the same amount of hp you had last time you played.

I want it to go back to maximum health at every start-up, though not when the next scene starts. Protect it by using DontDestroyOnLoad between scenes.

Only save health when saving the state of progress. Therefore, it makes sense that there can only be 1 instance of the variable. Attachments: Up to 2 attachments including images can be used with a maximum of To help users navigate the site we have posted a site navigation guide. Make sure to check out our Knowledge Base for commonly asked Unity questions. Answers Answers and Comments. How to write If the player don't play the game for 24 hours, the game is over.

Distribute terrain in zones 3 Answers. How to Load PlayerPrefs from a different scene 1 Answer. How do I load a new scene based on current scene? How to pass array variables from one script in one scene to another script in another scene? Login Create account. Ask a question. GetKeyDown KeyCode.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here.

Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I made two scripts. One that'll keep track of the player's health, health bar and cause the screen to flash when the player is damaged. The other script is meant to be placed on any object I wish to do damage to the player, on contact. My problem is, Nothing seems to be doing any damage to the player. Instead of GameObject references rather compare the PlayerHealth since there is only one reference like.

I don't know what else should happen on TakeDamage but the value of damaged is resetted in Update so right after it was set by the Trigger because Physics events like OnTriggerEnter are executed before Update see execution Order. Or to be more flexible having multiple Players all you have to do is change your OnTriggerEnter2D and Attack method to.

Learn more. Cause player to take damage in Unity 2D Ask Question. Asked 1 year, 5 months ago. Active 1 year, 5 months ago. Viewed 2k times. The player and damaging objects have Box Collider 2D's on them. Are your damaging object has "Is trigger" checked on box collider 2D? There is good table when collision activating docs.

Also have you debug it? Does OnTriggerEnter2D fires at all and something wrong with rest part of the scripts, or collision does not even activate it? Looks like it should work. Inside the OnTriggerEnter2D, try doing if col.

Active Oldest Votes. Make sure that the players Collider has isTrigger enabled. Make sure it is not 0. Sadly, right after setting "isTrigger", it started to work. I had separate issues I was able to work through after that. Thank you. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.Discussion in ' Scripting ' started by MorphisJan 28, Search Unity. Log in Create a Unity ID. Unity Forum. Forums Quick Links. Asset Store Spring Sale starts soon!

Unite Now has started! Come level up your Unity skills and knowledge. Joined: Jan 27, Posts: So I have created a player health script it works great with enemy's that i hand put in the game if I collide with an enemy or if it shoots me I loose health but when I use my spawner to spawn random enemy's and I collide with and enemy I am getting a NullReferanceException taking me to this.

Code CSharp :. TakeDamage attackDamage. Collections. FindWithTag "GameController".

unity 2d player health script

Log "Cannot find 'GameController' script". CompareTag "Boundary" other. CompareTag "Enemy". Instantiate explosion, transform. Range 150. Log number. Attack. Instantiate playerExplosion, transform.

GameOver. Destroy gameObject. AddScore scoreValue. PNG File size: MorphisJan 28, Joined: Mar 16, Posts: 5, Sounds like the spawned enemy may not have all its properties filled out, like you said, the player or playerHealth and so on.

unity 2d player health script

To figure out which one it is, look at the line number on the error you're getting. Now in order to fix it, what you need to do is when you Instantiate spawn the new enemy, get a reference to its GameObject, use.

One handy way to accomplish all this is to have a reference to the player in your Spawner script, and then when the Spawner spawns an enemy, it has the required information to set up the new enemy properly.

Kurt-DekkerJan 28, Morphis likes this.You must register to post or download hacks. Follow MPGH. Remember Me? Game Development. Unity Enemy AI, health and attack scripts C. Results 1 to 11 of Thought I would release these, there just simple script and I'm just learning.

So please no hate? I have no clue why it's censoring. Last edited by pspdude; at PM. Very impressive. Nice job.

Enemy health

What work have you done in Unity? I'm kinda interested? Originally Posted by OBrozz. Last edited by pspdude; at AM. Derp 77 views and only one person rplied Feel free to add me on steam: Dvorak. This isn't a really good AI, All its going to do is calculate how far away it is and the rotate towards you and move there.

You need to factor in being on a higher ground than the enemy or lower. I might release some paid unity assets soon. Gratz on learning from tutorials but if your going to release some code make tweak it up at abit. Like your Health script which is word for word from this tut 2.In this section of the tutorial we will create a player object that we can control with the keyboard or controller.

The first step is to right click in your Hierarchy view. This will bring up a menu where we can add all the things. This will create a new item in the hierarchy. With the new object selected in the hierarchy view press F2 or right click rename to rename the object to PlayerObject.

This will be the root element of our player. It will hold all the objects that make up our player such as sprites. Now, lets add a sprite to our PlayerObject so we can see our player in both the Scene view and game view. This is due to the default sprite not having any image associated with it. We will add that now. Select the Knob no giggling by double clicking it or hitting the enter key with it selected. In order to do that we must add some code.

Here we can add all sorts of prebuild components but for now lets just add a Player script. Type Player in the search bar at the top to get selection of Video Player or New script. Select new script and click the Create and Add button to make the script for us. We now have a script in our project called Player. Eventually we will have a lot of scripts and it will become hard to find things in the Project window so lets make things easier for our future selves by creating a folder and calling it Scripts.

This will house all our future scripts and keep the Project view organized for us. Double clicking our Player script should open visual studio with the Player script already open in the code view window. When a script is first created you are given 2 methods; Start and Update. The Start method is called once when the object is created and Update is called once per frame after.

This allows us to setup the player with values once in the start method and then update the player in the Update method from then on. In the first part of our code add these two variables. These will be used to set our player speed and health.

As these variable have been declared with public so we will be able to see the in the unity Inspector view and we will be able to change them from inside unity. Here we have used the Input. This value will be -1 if the A or left arrow key is pressed, 1 if D or right arrow is pressed and 0 if neither are pressed.

Next we convert this raw input value into a value effected by the moveSpeed. Since this Update method is run every frame we want to use the Time. Without using Time. Next we create a Vector3 called directionOfMovement which will hold both our horizontal and vertical movement in a single variable.

A Vector3 is more than just a variable, it is a position or direction in 3D space. Why are we using something for 3D in 2D, well in short unity still works in 3D even when using 2D it just shows us the 3D world in 2D. This is why we left the 3rd parameter in the Vector3 as zero.Discussion in ' Scripting ' started by s3ntryf0xJul 14, Search Unity. Log in Create a Unity ID.

How to Damage the Player Health Unity 5 C# Tutorial Easy

Unity Forum. Forums Quick Links. Asset Store Spring Sale starts soon!

2D sidescroller health bar

Unite Now has started! Come level up your Unity skills and knowledge. Need a player health and damage script please!!! Joined: Jul 11, Posts: 1. Ok so i've been trying to create player heath script but it doesn't work all. So if someone could make a player health script that can be damage and when the health goes to the bottom the player will be replaced by a different game object please it would really help.

Joined: Oct 2, Posts: 1, You should never ask for scripts. Post what you got so far, and then people can help you out and give you hints! BFGamesJul 14, Joined: Sep 29, Posts: BFGamesgiving scripts isn't idealbut I know that people learn from looking at other scripts and trying to work out what they do and how they are coded. Here's a simple script of what you are askingit's nothing too fancy.

Code csharp :. Destroy collision. Destroy Character. Joined: Nov 19, Posts: What for? It helps if you write down on paper what you want before trying to code it What else do you need? You need to get hurt and to die An example method to get hurt KillPlayer You will need to store the new dead model as a variable -- As you have identified you need a new variable, you can add it to your list up the top now!

Instantiate the new model separate game object will keep it there if you delete 'this' game object Instantiating a prefab will also mean you can have scripts attached to it from the start to give it the behaviour you want eg.

GameObject deathModel.

unity 2d player health script

Remember to have a check for HP below zero or above maxHP somewhere doesnt have to be here. Look through the above stuff. Last edited: Jul 14, MickMJul 14, You must log in or sign up to reply here. Show Ignored Content. Your name or email address: Password: Forgot your password?I have the health bar appearing on my screen, but I am not sure how to link the Health script of the health bar to my player script and my player's health script.

Simply, I want to make it so when my player gets shot my health bar will lose a heart. The health script of the player's is also linked to the enemy. I used this video for the health bar and now I need to figure out how to incorporate the health bar script with my health script and and player script.

I'm not keeping track of lives but hitpoints, the player has a points of health and a each heart equals 10 points of health.

Attachments: Up to 2 attachments including images can be used with a maximum of To help users navigate the site we have posted a site navigation guide. Make sure to check out our Knowledge Base for commonly asked Unity questions. Answers Answers and Comments. Scaling related to damage 2 Answers. Health subtracts when hit by bullit 2 Answers. Login Create account. Ask a question. I am new to coding and Unity I have the health bar appearing on my screen, but I am not sure how to link the Health script of the health bar to my player script and my player's health script.

Health script, save health in between scenes. PlayerPrefs.

Add comment. Best Answer. It worked! You must be an angel man, I've been stuck on this forever! Thanks a lot! Your answer. Hint: You can notify a user about this post by typing username.