There are actually at least two problems:
- Your trying to access the text, but your only referencing the container.
- 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
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
- 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:
- 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.
- 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.