- Home /
Disable Mesh Renderer but use it's GUI
Hi,
function OnTriggerEnter(other : Collider){
if (other.gameObject.CompareTag("RocketTag")) {
s1_02Fuel.Score += 10.0;
renderer.enabled = false;
audio.PlayOneShot(CoinSound);
CoinCought = true;
}
}
function OnGUI ()
{
GUI.skin = GUICoin;
if (CoinCought == true)
{
GUI.Label (Rect (270, y, 880, 100), "+10");
y += -100*Time.deltaTime;
}
}
My problem is. If the mesh renderer is false. The GUI.Label won't show. What should I do to disable only the Mesh Renderer and show it's GUI.Label?
Hope you guy's understand me.
Thanks in advance
public var myGameObject : GameObject; // GameObject to disapear
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.CompareTag("RocketTag"))
{
myGameObject.renderer.enabled = false; //If script attach to GameObject ,that to disappear ,then you can use renderer.enabled = false;
CoinCought = true;
}
}
In this code "myGameObject" is the game object which need to disappear .
If you are attaching this script to game object which need to disappear then you can use
renderer.enabled = false;
Else if you are attaching this script to another game object which need to disappear then you can use
myGameObject.renderer.enabled = false;
Also you check you declared variable " y " as
var y : float ;
As you can see, I have exactly the same js you wrote down in here. I attached the js on the object that should disappear. So I used renderer.enabled = false; This is the whole js
var CoinSound : AudioClip;
var GUICoin : GUISkin;
var CoinCought = false;
var y : float = 560;
function OnTriggerEnter(other : Collider){
if (other.gameObject.CompareTag("RocketTag")) {
s1_02Fuel.Score += 10.0;
CoinCought = true;
audio.PlayOneShot(CoinSound);
}
}
function OnGUI ()
{
GUI.skin = GUICoin;
if (CoinCought == true)
{
GUI.Label (Rect (270, y, 880, 100), "+10");
renderer.enabled = false;
y += -100*Time.deltaTime;
RemoveText ();
}
}
function RemoveText ()
{
yield WaitForSeconds (1);
s1_02Fuel.Score += 10.0;
CoinCought = false;
Destroy(gameObject);
}
Answer by Tomer-Barkan · Oct 24, 2013 at 05:08 AM
Mesh renderer has no effect whatsoever on GUI.Label
.
Try this simple test and you'll see:
public class ShowLabel: MonoBehaviour {
void OnGUI () {
GUI.Label(new Rect(10, 10, 300, 50), "Test");
}
}
Attach the script above to a cube, disable the renderer, and you'll still see the text.
Try the following to debug your problem:
Make sure the object itself is not disabled, and the script is not disabled, otherwise the script won't run.
Try to check your "y" value, by using
Debug.Log
. If it's < 0, that would explain, you're drawing outside of the screen.Try removing the
if
statement. In order for a GUI.Label to work, you need to draw it every frame. If at one frame you do not call theGUI.Label()
command, it will not be drawn in that frame. (Yes, GUI.Label is not the most efficient way to go).Consider using GUIText instead of GUI.Label. It's more efficient and easier to use:
http://docs.unity3d.com/Documentation/Components/class-GuiText.html
just a Cube I created, and disabled the renderer, but any object will do as long as you enable the object itself and the script
place and check line CoinCought = true (no. 7) before renderer.enabled = false;
As you can see my js. I have the gui label. But if the renderer is false. The gui label won't show up. I can't disable the mesh renderer in the inspector. Because I need to take that object. Hope you understand.
Your answer
Follow this Question
Related Questions
Enable/Disable Game Object With GUI Button 1 Answer
Turning off ONE GUI button 1 Answer
Re Enable C# script on camera 1 Answer
Activating GUI 2 Answers
Enable a GUI texture after player collides with another object 2 Answers