- Home /
How to edit player health based on the player level
Hopefully I won't get in trouble for this, a moderator declined this last night, but I don't see why, so I am trying again.
tried making something, but it says I can't turn a float into a bool, so I don't know how to do that. I have 2 scripts, one that declares XP and Level and related items, and one that declares attributes. I want to make the Attributes depend on the player level. I tried putting a code into Update, but it just makes the player's health go up a bit every frame. Help?
Here is the PlayerLevel code (IK it doesn't look pretty, I am still new to coding)
using UnityEngine;
using System.Collections;
public class PlayerLevelDeclaration : MonoBehaviour {
public float PlayerLevel = 1;
public float XP = 0;
public float DroppedXP = 0;
public float NeededXPUntilLevelUp = 100;
public float LeftoverXP = 0;
public float newXP = 0;
// Use this for initialization
void Start () {
Debug.Log (LevelUp());
}
// Update is called once per frame
void Update () {
LevelUp ();
AddXP ();
}
public float LevelUp(){
newXP = XP;
if(XP >= NeededXPUntilLevelUp)
{
//XP = 250, NeededXPUntilLevelUp = 249, then hopefully this sets xp to one, and increases the player level by one
newXP = XP - NeededXPUntilLevelUp;
LeftoverXP = newXP;
PlayerLevel = PlayerLevel + 1;
XP = newXP;
NeededXPUntilLevelUp = NeededXPUntilLevelUp + (PlayerLevel * 3.2f);
}
XP = newXP;
return PlayerLevel;
return XP;
return NeededXPUntilLevelUp;
}
public float AddXP()
{
#region TestingForFuture
if(Input.GetKeyDown(KeyCode.J))
{
XP = XP + 50;
}
return XP;
#endregion
}
}
The Attribute script (health)
using UnityEngine;
using System.Collections;
public class AttributeDeclaration : MonoBehaviour {
#region Public Floats
public float Health = 100;
public float Magic = 100;
public float Strength = 100;
public float Stamina = 100;
public float Defense = 100;
#endregion
PlayerLevelDeclaration PT = new PlayerLevelDeclaration();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(PT.PlayerLevel =+ PT.PlayerLevel + 1){
Health = Health + (PT.PlayerLevel / 2f);
}
}
}
Thanks in advance. In case I didn't clarify my problem above clearly, I basically want the player's health to go up a little, every time the Player's level goes up. I basically want to update the players health, if the player's level goes up. (Please don't get very complicated, and if you do, please explain. I am very new to coding still) (Someone said properties, but I have no idea how to use them.)
EDIT: I formatted the code with the 101 010 button, I don't see what it did, but I did it. The error log in the console is:
Assets/Scripts/AttributeDeclaration.cs(22,17): error CS0029: Cannot implicitly convert type float' to
bool'
it also says:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all -(This occurs when I create an instance of PlayerLevelDeclaration inside of my attribute script)- -(I put this in my attribute script, and that error/warning happens: PlayerLevelDeclaration PT = new PlayerLevelDeclaration(); )-
It also says: Assets/Scripts/PlayerLevelDeclaration.cs(38,17): warning CS0162: Unreachable code detected as a warning
If you need anything else, let me know.
Also, when I comment out "if(PT.PlayerLevel =+ PT.PlayerLevel + 1){ Health = Health + (PT.PlayerLevel / 2f); } " in my Attribute script, the error goes away but the warnings are still there.
Answer by Sir-Irk · Nov 17, 2014 at 09:54 PM
Assets/Scripts/AttributeDeclaration.cs(22,17): error CS0029: Cannot implicitly convert type float' to bool'
if(PT.PlayerLevel =+ PT.PlayerLevel + 1){
This is not a true or false statement.
If you want to see if the player's level is higher than before you need to keep a separate variable that will keep track of what the player's level was last time the Update() method was run(Or use a Property). I would also suggest changing PlayerLevel to an int. So something like this in AttributeDeclaration:
private int previousLevel = 0; //Have this as a field so that it has class-wide scope.
private void Update(){
//PT.PlayerLevel is increased somewhere else in code...
if(PT.PlayerLevel > previousLevel){
//Increase health code
}
previousLevel = PT.PlayerLevel;
}
Another way is to have PlayerLevelDeclaration cache AttributeDeclaration. Then when the player levels up, run a method in AttributeDeclaration. For Example in PlayerLevelDeclaration:
private AttributeDeclaration healthScript = null;
private void Awake(){
//Assumes that 'AttributeDeclaration' has already been added to the same GameObject
healthScript = GetComponent<AttributeDeclaraction>();
}
private void LevelUp(){
//...level up code
if(healthScript != null){
healthScript.IncreaseHealth()//Make this method whatever you need it to be.
}
}
These are dead simple examples. There are other ways of achieving the same thing. I would also suggest better naming for your scripts. You probably don't need "Declaration" in there.
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all -(This occurs when I create an instance of PlayerLevelDeclaration inside of my attribute script)- -(I put this in my attribute script, and that error/warning happens: PlayerLevelDeclaration PT = new PlayerLevelDeclaration(); )-
The error tells you exactly what's wrong. If you were trying to add that MonoBehavior to the object then use AddComponent() just as the error says. If you were just trying to cache PlayerLevelDeclaration(meaning you've already placed this component on the object in the editor or with another script) then use GetComponent();
Example:
PT = GetComponent<PlayerLevelDeclaration>(); //Assuming it is on the same GameObject.
It also says: Assets/Scripts/PlayerLevelDeclaration.cs(38,17): warning CS0162: Unreachable code detected as a warning
This is because you have return statements that can't possibly be executed no matter what happens when the code is running. The return statement terminates the execution of a function and returns control to the calling function. If you need to return multiple variables then look into the out keyword.
How can I make that a true or false statement? I only want to run that chunk of code if the PlayerLevel increases.
I've edited my answer to give you two methods of doing what you need.
Thank you so much. I found a way to do it as well, and i think one way is a way you are explaining, I am basically doing what blueLED said. I know my names all suck, and it is probably very inneficient, but this isn't my actual game, I'm still learning coding, just screwing with stuff to see what I can do.
Answer by blueLED · Nov 17, 2014 at 10:40 PM
How about you just put your health increase here? When the LevelUp happens.
if(XP >= NeededXPUntilLevelUp)
{
//XP = 250, NeededXPUntilLevelUp = 249, then hopefully this sets xp to one, and increases the player level by one
newXP = XP - NeededXPUntilLevelUp;
LeftoverXP = newXP;
PlayerLevel = PlayerLevel + 1;
XP = newXP;
NeededXPUntilLevelUp = NeededXPUntilLevelUp + (PlayerLevel * 3.2f);
}
Thank you, I selected the other answer because it gave me more information and methods and such, but this did help me. Thanks.
No worries.
Also, this may help you:
PlayerLevel = PlayerLevel + 1;
can be written as
PlayerLevel++;
and
XP = XP + 50;
can be written as
XP += 50;
Your answer
Follow this Question
Related Questions
How to implement an attack in this script 1 Answer
I"Expressions in statements must only be executed for their side effects." 0 Answers
Health System Help 2 Answers
How can I do health bar follows enemy? 1 Answer
Checking for a value overtime 2 Answers