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?

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.

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;

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

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