I have a prefab with the following component structure:

  • Block
    • Quad
    • RawImage
    • GUIText

When I instantiate it and try to access the GUIText, I get the output of 'null'.

Here is the code, I am using to access the GUIText:

public GameObject Block;

GameObject obj = Instantiate(Block, position, orientation) as GameObject;
GUIText myText = obj.GetComponent<GUIText>();

How do I fix this?

  • 1
    \$\begingroup\$ What version of Unity are you using? It may be relevant. It has been pointed out that GUIText is legacy; regardless, it should still work as your intending to use it. \$\endgroup\$ – Gnemlock Jan 7 '17 at 23:11
  • \$\begingroup\$ I'm using Unity 5.3 on Windows 10 \$\endgroup\$ – Pritesh Desai Jan 8 '17 at 21:21
  • \$\begingroup\$ @Gnemlock <Facepalm> You were right, I was trying to access it as GUIText when it was a simple Text. \$\endgroup\$ – Pritesh Desai Jan 9 '17 at 20:55

There are actually at least two problems:

  1. Your trying to access the text, but your only referencing the container.
  2. There still does not appear to be a GUIText component attached to your game object.

First and foremost, GUIText is a class that contains the text as one of the variables. If you want to access the text, itself, you need to use GUIText.text.

That said, that does not account for GUIText outputting a string value of null. Unfortunetly, the only thing that really does is if GetComponent<GUIText> initially returns a null reference. It is hard to say why this is the case, but ultimately, it does not sound like there actually is a GUIText directly attached to the game object.

It is difficult to assess the exact cause as of this point, but there are a few possible causes and solutions.

  • Perhaps you mean to say that Block contains a child that contains a GUIText? We need to use GetComponentInChildren(type), if that is the case.
    • Note that this works off the assumption that there is still only one GUIText in the entire hierarchy of Block. If this is not the case, you will have to further ensure you have the correct reference.
    • You will also note that this method does not use a generic (<T>). Instead, you would write it as GetComponentInChildren(type of(GUIText)).
  • Perhaps, through human error, your actually referencing an object or instance that does not have a GUIText component. If you want to account for the possibility, which is considered a good practice, you have two options:
    1. Place any code accessing myText inside an if statement that checks that myText != null. Remember, trying to retrieve a component that does not exist will not cause errors, but further access will. You can have an else statement follow, if you want somewhere to put debug output if the check fails.
    2. If you must have a GUIText object directly on that object, you can mark use the [RequireComponent] attribute on a script that is attached to that object. Simple include [RequireComponeny(type of(GUIText))] directly above the main class, and if your object does not have one, the class will automatically create one. If you do not have a script attached to the object that is unique to the object (or rather, unique to game objects that should contain a GUIText component), create one just for this purpose.
  • Failing all this, it might be worth setting up the GUIText as a pre-setup public reference on a local script. This script would be attached to your prefab object, ideally with all required references set up, internally. You would than grab a reference to the script using GetComponent<T>, as you are, now. The core difference is that you would only have to do it once to have the access you require, assuming you require reference to the other components. This also acts as a way around issues that may be isolated to the exact way you have the GUIText set up.

Do not use GameObject.Find to clarify your reference to the initial game object, as suggested by an alternate answer.

  • GameObject.Find can be slow. It is not ideal, especially if you frequently run this code.
  • GameObjevt.Find returns the first game object that is found to match. There is no guarantee which instance this will be, if you have multiple, so it can be especially unreliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.