- Home /
Display GUI Text
I am creating an anatomy app. I have assigned mesh colliders to each muscle so that once clicked guitext will appear displaying the name of the bone or muscle. Here is an example of that when I clicked the Left Glute:
Now getting the guitext to display was actually quite simple, I created a GameObject and named it Muscle Text and added a GUI Text.
I then created a script for each muscle:
using UnityEngine;
using System.Collections;
public class Glutes: MonoBehaviour {
public GUIText Muscle;
void Start ()
{
Muscle.text = "";
}
void OnMouseDown()
{Debug.Log ("Clicked on the muscles");
Muscle.text = "Glutes";
}
}
And it works great, but my problem is this will be quite time consuming since there are hundreds of muscles and bones and creating an individual script for each one would be far to tedious and impractical. How can I bulk up my original script for this to be far more efficient?
Answer by fishbrainz · Apr 21, 2016 at 12:43 PM
using UnityEngine;
using System.Collections;
public class BodyPart: MonoBehaviour {
public GUIText TextOutput; //set in inspector
public string Name; //set in the inspector
void Start ()
{
TextOutput.text = "";
}
void OnMouseDown()
{Debug.Log ("Clicked on the muscles");
TextOutput.text = Name;
}
}
Add this to every Muscle/Bone gameobject, set the references to the TextOutput and you should be good to go.
In general when in the situation where you say "This works but I have to do this so many times for so many things" try to think of a generic way like this to do it.