Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Karsten · Apr 23, 2012 at 05:57 AM · nguilabelpublicmember

NGUI score counter

Hello,

How can you access the UILabel.text member from within some script in your project?

Scenario: you have a prefab instance with a script attached and from that script you want to set the text of a UILabel the Label is in the same scene like the prefab instance.

in short, how to find as fast as possible a specific instance of an UIlabel to access its .text member from within a script that is attached to another object?

i hope i could be understood please help, im going mad trying everything like GetComponent("nameoflabel") and so on How,please?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dannyskim · Apr 23, 2012 at 09:07 PM

You're thinking about it just slightly wrong. UILabel is a component of NGUI, so all UILabel widgets are going to have the UILabel script. So when you're doing a GetComponent of a UILabel, it is always going to be GetComponent(UILabel).

 UILabel c = GameObject.Find("Your Object Name Here").GetComponent(UILabel);
 c.text = "The String You Want To Assign";

As a note, NGUI was designed with extensibility in mind. When you cache the UILabel into a variable, it also gives you access to the GameObject itself, and everything that is inherited from it ( transform, rigidbody, etc ). Since everything is inheriting from something else, it may be something you want to consider in your design and declaration process.

Comment
Add comment · Show 2 · 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 Bunny83 · Apr 23, 2012 at 09:15 PM 1
Share

Looks like you've mixed Unityscript with C# ;) Your variable declaration is C# but the GetComponent call is UnityScript.

 //C#
 UILabel c = GameObject.Find("Your Object Name Here").GetComponent<UILabel>();
 
 // UnityScript
 var c : UILabel = GameObject.Find("Your Object Name Here").GetComponent(UILabel);
avatar image dannyskim · Apr 24, 2012 at 06:16 PM 1
Share

Thanks again Bunny, as you can see I clearly prefer C# over unity script =p

avatar image
1

Answer by Kid_Gloves · May 18, 2012 at 02:37 AM

Slight necropost here, but...

You're trying to access a child object of your InGameUI object via GetComponent. The ScoreLabel isn't a component, it's an entire object in itself which has (I assume) the UILabel component.

You can either try and reference that label directly, or you can create a component in a parent script that handles updating the labels, etc. in it's children. I'd go with the latter because it's more modular - so all score-related processes go to a score manager via an interface you design.

If you just want to access the UILabel component directly, your call would be:

UILabel c = GameObject.Find("ScoreLabel").GetComponent();

or

UILabel c = GameObject.Find("ScoreLabel").GetComponent("UILabel") as UILabel;

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 Karsten · Apr 27, 2012 at 02:46 PM

it dont works... look the screenshots, i want to access ScoreLabel from the Projectile script.the projectile script is generated dynamically so dont be confused, yes it WILL exist at runtime, its the 3dbuzz c# class maybe you know,but i was trying to alter it with NGui any idea how? please

alt text


screen1.png (130.3 kB)
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 Karsten · Apr 28, 2012 at 04:47 AM

ok to clear things up, i found a solution myself, but the question is if there are better,faster ways also the "problem" here is, that its a bit hardcoded, because if you change names of your components in editor you have to change your code. anyway here it goes, if you look my screenshot above you understand the code better.

c# private UILabel scorelabel;(in class head)

GameObject obj = GameObject.Find("IngameUI/Camera/Anchor/Panel/ScoreLabel"); scorelabel = (UILabel)obj.GetComponent("UILabel"); scorelabel.text = "Any text";

where as you can see scorelabel is a member variable of type UILabel in the current Class

thanks to all the readers and writers

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Ngui Text Vertical Scrollbar? 1 Answer

Ngui: how to insert text on a label via javascript? 1 Answer

Positioning Label with NGUI 1 Answer

NGUI - move label in absolute pixels 0 Answers

NGUI countdown in label 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