Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tubestorm · May 11, 2020 at 02:54 PM · stats

calculating stats for player Need Help

Hello I'm creating a similar game to bit life, but i'm having issues finding any good resources that helps.


I'm trying to make stats that can be applied to the player and the NPCs when they are generated. There should be 4 - Joy, energy, brains and appearance.


I started off with energy, because when the character moves around I want the energy to reduce. and when they eat or stop moving, their health increases. So far below is the code i've got.

This class should calculate the amount of Energy left, based on the maxEnergy and currentEnergy values sent from the player/NPC class.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnergyCal : MonoBehaviour
 {
     //VARIABLES
     public List<customSort> energySort = new List<customSort>();
 
 
     // Update is called once per frame
     void Update()
     {
         IsWalking();
     }
 
 
     public void IsWalking()
     {
 
         energySort.Add(new customSort("Default", 1, true));
         energySort.Add(new customSort("Walking", 2, false));
 
         //check if character is standing or walking
         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
         {
             energySort[0].isRemoving = true;
             if (Input.GetKey(KeyCode.LeftShift))
             {
                 energySort[1].isRemoving = true;
             }
             else
             {
                 energySort[1].isRemoving = false;
             }
         }
         if (Input.GetAxis("Horizontal") == null || Input.GetAxis("Vertical") == null)
         {
             Debug.Log("heloo");
         }
     }
 
     //CALCULATES THE REMOVED ENERGRY
     public int removeEnergy(int maxEnergy, int currentEnergy)
     {
 
         foreach (customSort e in energySort)
         {
             if (e.isRemoving)
             {
                 currentEnergy += (int)(e.value / 100);
             }
         }
 
         maxEnergy -= (int)(currentEnergy * Time.deltaTime);
 
         if (maxEnergy <= 0)
         {
             //die
             Debug.Log("YOU DIED");
         }
         return maxEnergy;
 
     }
 
 
 }
 



This is the stats Bar class.

 /*---- This file does:
  * Holds the status for all characters in the game
  */
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class StatsBar : MonoBehaviour
 {
 
     public StatsVariables charEnergy;
     public EnergyCal Energy;
 
    
     void Start()
     {
         charEnergy.Max = 100;
         charEnergy.Current = charEnergy.Max;
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         //taking damage from walking and prints out the value
         Energy.IsWalking();
         charEnergy.Current = Energy.removeEnergy(charEnergy.Max, charEnergy.Current);
         Debug.Log(charEnergy.Current);
     }
 
 }
 



I keep getting this error when I add the stats class to my character, when he moves around the value doesn't reduce aswell. Any ideas?

NullReferenceException: Object reference not set to an instance of an object StatsBar.Update () (at Assets/Scripts/Character Scripts/StatsBar.cs:28)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bombombambam · May 11, 2020 at 02:59 PM

You have to assign charEnergy and Energy in the inspector

Comment
Add comment · Show 1 · 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 Tubestorm · May 11, 2020 at 03:13 PM 0
Share

Hello, charEnergy and Energy are class variables.

so i can't assign them in the inspector.alt text

screen-shot-2020-05-11-at-110823-am.png (35.6 kB)
avatar image
0

Answer by Tubestorm · May 11, 2020 at 04:07 PM

@Babish8

Hello, i'm getting this errors, i don't think changing it to static worked.

 Assets/Scripts/Character Scripts/EnergyCal.cs(28,13): error CS0120: An object reference is required for the non-static field, method, or property 'EnergyCal.energySort'



I don't think i'm going about this the right way. I'm trying to create a stats class that can be used for all characters, including NPCs (as i plan to randomize these values and create multiple random NPCs). Would you suggest a different way to code the rpg stats.

Comment
Add comment · 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

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

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Controlling One of Several Similar Playing Pieces 1 Answer

What would be the best approach for regenerating stats? 1 Answer

My character stats script doesn't identify my variables 3 Answers

Getting XP when dies Help 1 Answer

new user very easy c# question regarding syntax 1 Answer


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