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 max-hatfull · Jul 19, 2016 at 09:27 PM · c#uitext

UI Text not displaying public int but will display private int

I am trying to display the value of a public int that I can set in the inspector but it won't show, if I use a private int instead it's all fine. If I make a private int and set it equal to my public int that doesn't work either. The really odd thing is that it DOES change the text in the inspector, just not the game view! What is going on?!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PortalController : MonoBehaviour {
 
     public Text scoreText;
 
     public int totalPortalPoints;
     private int portalPoints;
 
     void Start()
     {
         portalPoints = totalPortalPoints;
         Debug.Log("Portal points at: " + portalPoints.ToString());
         scoreText.text = "Portal points at: " + portalPoints.ToString();
     }
 
     void OnTriggerEnter(Collider other)
     {
         GameObject creep = other.gameObject;
         CreepController cc = (CreepController)creep.GetComponent("CreepController");
         portalPoints -= cc.portalPoints;
         Debug.Log(portalPoints); 
         Destroy(creep);
     }
 }
 
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 Jessespike · Jul 19, 2016 at 09:50 PM 0
Share

Not really sure what you mean, sounds like you want UI Text to always show the value of totalPortalPoints. But it will only set the text once, because it happens at Start and no where else. What if you change Start to Update?

  void Update() {
      scoreText.text = "Portal points at: " + totalPortalPoints.ToString();
  }
avatar image max-hatfull · Jul 19, 2016 at 09:53 PM 0
Share

I will eventually want it to change at specified times but for now I want to get this working at the start

avatar image max-hatfull · Jul 20, 2016 at 11:16 AM 0
Share

Even with your change it just says "Portal points at: " although in the inspector it has it with the number too

avatar image pako · Jul 20, 2016 at 11:58 AM 0
Share

I copied the code you have in Start() in a new script, added an empty GameObject and attached the PortalController script. I also created a Text object and dropped it in the scoreText field in the Inspector.

Everything worked just fine. Any value I put in the Inspector for totalPortalPoints (before clicking Play) was shown in the text field and in the console (after clicking Play).

Funny thing...

avatar image max-hatfull pako · Jul 20, 2016 at 02:35 PM 0
Share

Seriously? Should I just reinstall unity? I can't for the life of me see what I've done wrong, it just puts half the string on the screen but says it has all of it in the inspector.

avatar image pako · Jul 20, 2016 at 12:02 PM 0
Share

I also added jessespike's Update() code, and every time I change the value of totalPortalPoints in the Inspector, the value of scoreText in Game View changed immediately.

Apparently there is something wrong with your setup.

1 Reply

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

Answer by max-hatfull · Jul 20, 2016 at 02:43 PM

I fixed the issue by putting the code to update the text in it's own method. I have no idea why this works, just glad it does.

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

207 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score UI per player 1 Answer

how to connect my Health to my UI 1 Answer

Using PlayerPrefs to store string but it won't update on my UI 1 Answer

Access parts of different scripts? 1 Answer

Text Objects do Lerp 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