- Home /
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?
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.
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);
Thanks again Bunny, as you can see I clearly prefer C# over unity script =p
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;
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
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
Your answer
Follow this Question
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