How to use a public int in another script?

Hello,

I have a script, which will display on the menu your total coins.

In my game, I have a score, and when you die, the score must be updated to your coins.
Now what I have done, is when you get a point for your score, it also needs to be updated directly to the total coins. So the same OnTriggerEnter2D function.

This is my coins script:

public static Coins C;
public Text coinsText;
public int coins;

// Use this for initialization
void Start () {

	C = this;
	coinsText.text = " " + coins;
	PlayerPrefs.SetInt ("coins",1);
	Update();

}

// Update is called once per frame
void Update () {

	Score.HS.OnTriggerEnter2D ();
	coins = PlayerPrefs.GetInt("coins");

}

And this is my Score script:

public static Score HS;
public Text scoreText;
public Text HighScore;
public int krolschValue;
public int score;

Coins int coins;

int highScore;

void start () {
	HS = this;
	score = 0;
	UpdateScore ();
	highScore = PlayerPrefs.GetInt("HighScore1",1);
}
public void OnTriggerEnter2D () {
	score += krolschValue;  
	coins += krolschValue;
	UpdateScore ();
}
public void UpdateScore () {
	scoreText.text = "X " + score;
	HighScore.text = "HighScore: " + highScore;
}
public void CheckHighScore()
{
	if(score > highScore)
	{
		Debug.Log ("Saving Score");
		PlayerPrefs.SetInt("HighScore1",score);
	}
}

Now I want the public int coins, from the coin script to the score script, so I can set into the OnTriggerEnter2D function that the coins also should get the value from the score on every point you get.

What I have right now doesn’t work, I have set Coins int coins, from the coins script.

You need to access the script that is attached to another GameObject, the easiest way to this is the following:

GameObject with your UI Script

public Text coinsText;
public int coins;

GameObject with your Score logic

public Coins UIgameObject;
public int score;
public void OnTriggerEnter2D () 
{
     score += krolschValue;  
     UIgameObject.coins += krolschValue;

     UpdateScore ();
}

The “trick” lies inside the Editor, select the the Score Logic GameObject that is inside the Hierarchy window, you will se a field that expects your UI/Coin script, drag the GameObject that has the UI/Coin script to that field, this way the Score Logic will have a pointer to access the script inside the code.

This is Unity 101 I recommend you check the tutorials available in Unity3D.com