Accessing a variable from one script in another.

Hey guys,I started coding a few months ago and I’ve been creating a simple game that features a JRPG style of combat. I need to keep track of all the different variables (action points, health, enemy health) and use them in other scripts, but I have no idea where to begin.

using UnityEngine;
using System.Collections;

public class FightDraft1 : MonoBehaviour 
{
	public float playerHealth = 100.0f;	
	public float enemyHealth = 100.0f;
	public float actionPoints = 10.0f;
	public float punchDamage = 5.0f;
	public bool turnEnd = false;
	public bool enemyDead = false;
	int raiderDamage;
	int raiderHit;
	int messagePlay;
	int playerHit;
	int playerDamage;
		
	void Start()
	{

	}
		
    void OnMouseDown()
	{
		playerHit = Random.Range (0, 11);
		playerDamage = Random.Range (5, 11);
		print (playerDamage);
		print (playerHit);
		if (actionPoints > 0.0f) 
		{
			if (playerHit >= 5)
			{
			  enemyHealth = enemyHealth - playerDamage;
			  actionPoints -= 10.0f;
			  print ("You hit the raider.");
			  print ("The raider took " + playerDamage + " points of damage.");
			  print ("The raider has " + enemyHealth + " health points left.");
			  print ("You have " + actionPoints + " Action Points left.");
			}
			else
			{
			  print ("You missed the raider");
			  actionPoints -= 10.0f;
			}
		} 
		else 
		{
			print ("You have no remaining action points");
			print ("Ending Turn...");
			turnEnd = true;
		}
	}

	void Update()
	{
		if (turnEnd == true) 
		{

			raiderDamage = Random.Range (3, 11);
			print (raiderDamage);
			raiderHit = Random.Range (0, 11);
			print (raiderHit);

			if (raiderHit > 5) 
			{
				print ("The raider punches you");
				turnEnd = false;
				playerHealth = playerHealth - raiderDamage;
				print ("The raider done " + raiderDamage + " damage.");
				print ("You have " + playerHealth + " health remaining.");
				actionPoints = 10.0f;
			}
			else
			{
				print ("The raider missed");
				turnEnd = false;
				actionPoints = 10.0f;
			}
		}
		if (enemyHealth <= 0.0f)
		{
			enemyDead = true;
		    print ("The raider has died");
			enemyHealth += 1.0f;
			Application.LoadLevel("Fallout'77-worldMap");
	   }
		if (playerHealth <= 0.0f) 
		{
			print ("You Have Died.");
			Application.LoadLevel("deathScreen");
		}				
   }
}

In the code attached, the player clicks the punch button, and it works out whether or not he hit, how much damage he dealt, how much health the enemy has remaining, and how many action points the player has left.

Since I didn’t know how to reference the variables in other scripts, I also included the enemy turn in this script, though I would like to make it a seperate script.

I need to use these variables in other scripts in order to add other attacks and abilities.

Thank you for any help you can give.

If you want to get a reference to your ‘FightDraft1’ script, you can write

public FightDraft1 variableName;

in your script. This will create a reference box in the inspector, which you can drag the instance of the script you want into it.
To access the playerHealth for example, you would use

variableName.playerHealth

Good luck :smiley:

You should look at this tutorial:
https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

It explains how to store data between scenes and between game executions, but it can be useful to understand the logic behind it: having an object controlling all the data you want to keep and reuse.

If you want a simpler solution, you can look at this thread: