Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BramNH · Oct 19, 2015 at 08:49 PM · scripting problem2d gameintpublic variablecoins

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadDevil · Oct 20, 2015 at 05:48 AM 0
Share

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.

avatar image BramNH MadDevil · Oct 20, 2015 at 01:03 PM 0
Share

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.

avatar image Umresh · Oct 20, 2015 at 01:35 PM 0
Share

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

avatar image BramNH Umresh · Oct 20, 2015 at 03:27 PM 0
Share

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

avatar image Umresh BramNH · Oct 26, 2015 at 10:56 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges