hello everyone,
im trying to do charge attack for the players
so made a class in my “_Player” class which is called “PlayerStats”, where i want all players to have the same variables and functions, (which will be attached to an empty gameobject in the scene)
and then i created 2 instances of “PlayerStats” …
now on the script that will be attached to the player itself I want to call that function.
but im not sure how to do it exactly …
here is my both codes.
===== _Player =====
using UnityEngine;
using System.Collections;
public class _Player : MonoBehaviour {
public class PlayerStats{
public int _health = 100;
public int _energy = 100;
public int _life = 3;
public float chargeSpeed = 1.5f;
// Functions Section //
public void fCalculateCharge(){
float chargetime = 0;
while (Input.GetKey(KeyCode.X)){
Debug.Log("IS Calculating charge" + chargetime);
chargetime += Time.deltaTime * chargeSpeed;
}
}
}
public PlayerStats _1_playerStats = new PlayerStats();
public PlayerStats _2_playerStats = new PlayerStats();
}
and in my charging code (which will be attached to the player) i have:=
==== _Charging ====
using UnityEngine;
using System.Collections;
public class _Charging : MonoBehaviour {
public bool _isCharging = false;
// Update is called once per frame
void Update () {
// Charge //
_charging(/*here*/);
}
void _charging(_Player player)
{
if (Input.GetKey(KeyCode.X))
{
Debug.Log("function charging called!");
if (!_isCharging){
Debug.Log("checks if charging");
_isCharging = true;
player._1_playerStats.fCalculateCharge();
}
else
Debug.Log("else: changing is charging is true");
_isCharging = true;
}
}
}
1st im not sure how to proceed in this since i dont know who i’ll put as an arguement for the function … and if i used _Player._1_playerStats.fCalculateCharge();
in the function instead of passing arguments … i get an error which says “An object reference is required to access non-static member `_Player._1_playerStats’”
2nd/ is it practical to call the function in the update and in the function i have if-statement ? or should i put the if-statement in the update function?
it’s all cuz of my lack of experience in unity programming so bare with me please <3
and yeh PLEASE Feel free to comment on my programming style of any type !
Thanks all,
Regards,
MrHero