Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wizardman290 · Nov 17, 2014 at 09:05 PM · xpplayer-healthhealth

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wizardman290 · Nov 17, 2014 at 09:22 PM 0
Share

I edited it for you, if you need more, let me know.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wizardman290 · Nov 17, 2014 at 10:19 PM 0
Share

How can I make that a true or false statement? I only want to run that chunk of code if the PlayerLevel increases.

avatar image Sir-Irk · Nov 17, 2014 at 10:39 PM 0
Share

I've edited my answer to give you two methods of doing what you need.

avatar image Wizardman290 · Nov 17, 2014 at 11:12 PM 0
Share

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.

avatar image
0

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);
  
          }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wizardman290 · Nov 17, 2014 at 11:13 PM 0
Share

Thank you, I selected the other answer because it gave me more information and methods and such, but this did help me. Thanks.

avatar image blueLED · Nov 17, 2014 at 11:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges