GuiText enabled/disabled

I’ve looked around, but I can’t seem to find an answer to my specific problem. I believe I have everything squared away, but I’m getting this error:

BEFORE I continue, the problem I’m having is that I can’t get my GuiText to appear nor my gameobject to disappear. What I’m trying to do is when I click on a gameobject, I want the render of that gameobject to disable, then for my GuiText to appear.

MissingComponentException: There is no ‘GUIText’ attached to the “First Person Controller” game object, but a script is trying to access it.
You probably need to add a GUIText to the game object “First Person Controller”. Or your script needs to check if the component is attached before using it.
MatchBoxScript.Update () (at Assets/_Scripts/MatchBoxScript.js:13)

I’m not quite sure what this means. I tried to pair it up with the FPC… but no good.

#pragma strict

function Start () {

}

function Update () {

if (Input.GetMouseButtonDown(0)){       
			var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			var hit : RaycastHit;
			
			guiText.enabled = false;
			
			if (Physics.Raycast(ray, hit, 100)){ // 1000 or Mathf.Infinity should be enough !
				// what did the raycast hit ?
				Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
				Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );

				if(hit.collider.gameObject.tag == "match")
				{       
			
					guiText.enabled = true;
					Destroy(hit.collider.gameObject, 5);
					
					GameObject.Find("MatchText"). animation.Play("MatchAnim");
				}
			}
		}

}

1 Answer

1

Just as the error suggests: you probably don’t have a GUIText on the same game object as your FPC script.

Just after your pragma declaration, before your Start(), have this:

var myGUITextObject : GUIText;

And then in the inspector, drag+drop your GUI text game object (from the Hierarchy) into the slot on your FPC script.

Thank you! I do not know what my FPC script is however...? I have a FPwalkerEnhanced, but not sure what it's referring to...?

–

Your game object "First Person Controller" has a script on it (not sure what the script is) that is trying to access a GUI Text. The problem at the moment is that there is no such component on that game object so it comes up with that error. You either need to point the script at the right game object+component, or else add a new component to your existing game object. Pointing the script at a separate game object is easier, and it means you can more easily change either object in the future.

–

Okay, so I tried pointing the script to it... but it didn't work. So I just paired it to the FPC under the main character. Now when I click the matchbox, it takes a few seconds to disappear.... and No Gui text shows up? Haha I've been looking through the code and I know I don't have a disable render function... but I think I have a display Gui text..?

–