- Home /
GUI Text Showing Gameobjects information
hi all, i need to build a game that will be informative , so that when i get closer to some game objects , it will show information about these objects that will be displayed on a GUI Text . How to do that ? any hints?
if there is some scripts i will appreciate that.
Answer by Justin Warner · Jan 12, 2011 at 10:32 PM
Psuedo:
Give objects you want with info a tag as info.
Make an array of objects with that tag by doing FindObjectsWithTag("info"); (I believe).
Have a toggle key (I'd do toggle key with the way that I'm saying), for this check input API, and if player pushes the key, check distances between that person and the gameobjects with the tag, and if the player is within distance, than show the GUI txt, or you can have a child of the object, and enable it that shows a material with the info above it...
Sorry, this is kinda messed up, I was going to delete it but figured you might find some use out of it, so I'll just post it, see what you can get from it.
Hope it helps though!
Answer by Lab013 · Mar 18, 2011 at 06:51 AM
While Justin's idea would technically work, it would not be what I'd consider a good method.
Create a script called ObjectInformation. Have it look something like this:
var information : String = "";
Add this script to any object you want to have abstract information associated with it. Then in your player script, add this script:
function OnGUI() {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 100.0)) { // The last parameter is the distance you want to test for
var info : String = "size: ";
info += hit.transform.localScale;
info += "\nposition: ";
info += hit.transform.position;
info += "\nrotation: ";
info += hit.transform.rotation;
info += "\nabstract info: ";
var objInfo : ObjectInformation = hit.transform.gameObject.GetComponent(ObjectInformation);
if (objInfo) {
info += objInfo.information;
}
else {
info += "null";
}
GUILayout.Label("info");
}
else
GUILayout.Label("No object found.");
}
Please don't be just another dumb copy and paste dev with no talent, respect, intelligence, or pride. Read and try to understand the code also.
Answer by amr nasr · Jun 25, 2011 at 10:11 PM
Sorry i have been away from unity game engine but now i am back developing some games i have tried your script and it looks straight forward and simple , the usage of the ray cast is a smart idea from you .
Thanks man
Your answer
Follow this Question
Related Questions
[JS] How to display an Int variable through a GUI Text?? [Resolved] 2 Answers
How do I reference a UI text in Unity 5? (JS) 2 Answers
Score wont show 0 Answers
How to create a review of previous updates 1 Answer
Using AutoType script on GUI.Label? 1 Answer