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 /
avatar image
0
Question by anotherbeaver · Aug 26, 2019 at 01:32 AM · scripting problemprogramming

How do I make a variable work in two scripts?

I am using a tutorial by Brackeys for Unity and have decided to change it a little by having the score be based on the number of obstacles you hit. I have the collision counter on the player and want the Text in the UI to display it. How do I have the variable from the Player also work in the Text?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Stratosome · Aug 26, 2019 at 01:40 AM

Heyo,

Well, to have the text be able to update according to the value in your obstaclesHit variable (I'm guessing), you'd need a reference to the whole object/MonoBehavior that contains the variable. Or, alternatively, a reference to the Text object (which I like better at the moment not knowing the full context). I'd recommend something like this:


  1. In your manager script that holds your obstaclesHit variable, expose a class variable to the inspector so that we can drag in the UI Text object: public Text hitDisplay; or [SerializeField] private Text hitDisplay;.

  2. In the inspector, drag in the UI Text object into that variable spot of your manager script (or whatever the script is).

  3. Every time you update the obstaclesHit variable in your manager script now, if hitDisplay != null, set its text to perhaps the obstaclesHit value.


There are multiple ways to do this, but I feel that this may be the most standard way of doing it that isn't too complicated. If you have any further questions, or this doesn't work out for you, let me know!

Comment
Add comment · Show 5 · 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
avatar image anotherbeaver · Aug 26, 2019 at 01:47 AM 0
Share

So I've made a public GameObject in my PlayerCollsions script and dragged my Text in. From here how do I get the Text to actually display the collision count?

avatar image Stratosome anotherbeaver · Aug 26, 2019 at 02:02 AM 0
Share

If it is Unity's UI Text object, I'd suggest making that public GameObject ... into public Text .... You will probably need using UnityEngine.UI; at the top of the script. We only need the Text component to modify rather than a full GameObject. Now, at this point, all you've got to do is every time you change the collision count variable, update the text using something like this:

 hitCount++; // Example
 
 // Update the displayed hit count
 if (hitDisplay != null) {
      hitDisplay.text = "Hits: " + hitCount;
 }
avatar image anotherbeaver Stratosome · Aug 26, 2019 at 02:24 AM 0
Share

Thanks, it worked! But, could you explain to me why the if function has "hitDisplay != null"? What does that do? Can I not just have "hitDisplay.text = "Hits: " + hitCount;" as the update?

Show more comments

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

200 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How do I make an updatable newsletter? 0 Answers

Particle Shaders not rendering if Instantiated 0 Answers

VS code with unity not working. 1 Answer

How can I make Stream Writer overwrite a txt file? 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