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.
try making an instance of coin script in the score script.
if you are using c#, it can be done something like this,
public CoinScript _inst;
from _inst, you can access public variables of coin script from score script
drag drop the CoinScript object in the inspector.
Okay, so in the score script, I have set a public Coins (which is name of coinscript) _inst;
And in the void OnTriggerEnter2D, I have set:
coins += krolschValue;
but i doesn't work, it can't get the int coins.
You have to get the object in with coin script is attached.
 CoinScript _intstance;
  void Start()
 {
 _instance = GameObjectwithCoinScript.getcomponent<CoinScript>();
 }
and now you can access the coins
What do I need to get exactly in the void Start?
$$anonymous$$y GameObject where the Coins script is in, is called Coins$$anonymous$$anager
And the coins script is: Coins
How many scripts do you have ? and which script do you have coins (variable)? You have to get the game object whose variable you want to access.
Answer by Paulo-Henrique025 · Oct 26, 2015 at 11:06 PM
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
Your answer
 
 
             Follow this Question
Related Questions
Animator does not show up 0 Answers
Main Menu Help! 1 Answer
HighScore and int! 0 Answers
Input/Animation works fine in Play (Editor) but not in the build 0 Answers
Dialogue script shows previous sentences after a while 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                