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.