calling Charging function from another instance of a class

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

This is a very long version of the most common question: “how to access another script in Unity”. See this answer for example.

In regards to your specific case:

_Player is the name of the class. It is not an instance. And instance would be in pure C#: _Player player = new _Player(); (of course, this isn’t very valid, because _Player is a MonoBehaviour). But (for the sake or example) player is now the instance. Now you can call player._1_playerStats or whatever.

Let’s say your _Player script is attached to some GameObject somewhere in scene. Then add public Player player to your _Charging and drag&drop the first object into second one’s inspector’s “player” field. Now you can use it. The first game object with _Player would now be giving you an instance to use: charging(player);.

P.S. Good use of debug logs, keep that up.