Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Vencarii · Mar 01, 2019 at 11:35 PM · uieventschange variable

UnityEvents, I think I need help to fully understand

Hi there,

I try to change the text on a UI Text element when an int value in the Player object changes. But I can't get it to work, I think there's still a missing piece I don't get in regards to UnityEvents.

This is the script on my UI Text element:

 public class MyText : MonoBehaviour
 {
     public void ChangeText(int value)
     {
         GetComponent<Text>().text = value.ToString("##,### $");
     }
 }

And this is the code in my Player script that has to do with the UnityEvent:

 [System.Serializable]
 public class OnNumberChangeEvent : UnityEvent<int>
 {
 }
 
 public class Player : MonoBehaviour
 {
     private int number = 200000;
 
     public OnNumberChangeEvent numberChange;
 
     public void ChangeNumber(int newNumber)
     {
         // doing some other stuff...
         
         numberChange.Invoke(newNumber);
     }
 }

I dragged the MyText GameObject into the "Number Change (Int32)" area on my Player GameObject and chose the method MyText.ChangeText with one parameter. But the text on the Text UI doesn't change, can anyone help? (I also chose the MyText.ChangeText with zero parameters, same result.)

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

2 Replies

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

Answer by Vencarii · Mar 02, 2019 at 08:51 AM

Partly solved! But there's another problem now.

The UI Text element is called now, I didn't have a reference to the Text prefab in my Player prefab in the UnityEvent.

But now I have another strange problem. I debug the active state of the Text element:

 Debug.Log("My Text ActiveSelf? " + gameObject.activeSelf);
 Debug.Log("My Text ActiveInHierarchy? " + gameObject.activeInHierarchy);

I can debug it when pressing the M key and it is debugged when the element is called by the UnityEvent. Console output when I press M at different occasions:

 My Text ActiveSelf? True
 UnityEngine.Debug:Log(Object)
 
 My Text ActiveInHierarchy? True
 UnityEngine.Debug:Log(Object)

But when the UnityEvent calls the method on the Text Element, the debug output is:

 My Text ActiveSelf? True
 UnityEngine.Debug:Log(Object)
 
 My Text ActiveInHierarchy? False
 UnityEngine.Debug:Log(Object)


Any ideas? It's really bugging me that I can't get the reason for that.

SOLVED!

Ok I finally solved that problem! I've read a hint in another forum thread somewhere that took me in the right direction. The problem was that I tried to change the text on the prefab Text element and not on the instance in the Scene.

What I've tried so far (WRONG!):

 GetComponent<Text>().text = value.ToString("##,### $");

What I do now and what works (CORRECT!):

 FindObjectOfType<MyText>().GetComponent<Text>().text = value.ToString("##,### $");


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

Answer by JxWolfe · Mar 02, 2019 at 01:03 AM

you need to call the change text void.... public myText;

 public void ChangeNumber(int newNumber)
      {
          // doing some other stuff...
          
          numberChange.Invoke(newNumber);
          myText.ChangeText(newNumber);
      }

plus, it appears that you are doing a lot of unnecessary functions... just simplify those down to one function 'UpdateNumber' or something that changes number, updates text, and does anything else necessary.

Comment
Add comment · Show 1 · 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 Vencarii · Mar 02, 2019 at 08:46 AM 0
Share

When I would like to have everything in one method I wouldn't even need UnityEvents...

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

168 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

Related Questions

Creating a custom slider - should I be invoking a UnityEvent onValueChanged? 0 Answers

onClick.AddListener() delegates don't appear on Inspector Events 1 Answer

Changing ScrollView content from the script 0 Answers

IDragHandler goes through buttons 0 Answers

Passing an int to a function from an onClick event gives the same value on every button. 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