Skip to main content
extended answer
Source Link
user000user
  • 924
  • 2
  • 7
  • 16

There are basically two ways to access the GUIText.

One way would be a public reference in the class or, like you already did, GetComponent.

The advantage of using a public reference is, that you do not have to attach the timer on the GameObject with the GUIText component. You just declare the variable like so:

public class Timer : MonoBehaviour
{
    public GUIText timerText;
    
    ...
}

You can then replace all GetComponent calls with timerText. To set this variable, simply drag and drop the GameObject that contains the GUIText component onto the Timer Text field in the Editor.

The GetComponent method, requires you, that the Timer script is attached to the same GameObject that contains the GUIText, or it is called on a GameObject on which has GUIText attached. It is also recommended, to use GetComponent just once, in the Start function of your Timer class, and store it in a variable.

There are basically two ways to access the GUIText.

One way would be a public reference in the class or, like you already did, GetComponent.

The advantage of using a public reference is, that you do not have to attach the timer on the GameObject with the GUIText component. You just declare the variable like so:

public class Timer : MonoBehaviour
{
    public GUIText timerText;
    
    ...
}

You can then replace all GetComponent calls with timerText. To set this variable, simply drag and drop the GameObject that contains the GUIText component onto the Timer Text field in the Editor.

The GetComponent method, requires you, that the Timer script is attached to the same GameObject that contains the GUIText. It is also recommended, to use GetComponent just once, in the Start function of your Timer class, and store it in a variable.

There are basically two ways to access the GUIText.

One way would be a public reference in the class or, like you already did, GetComponent.

The advantage of using a public reference is, that you do not have to attach the timer on the GameObject with the GUIText component. You just declare the variable like so:

public class Timer : MonoBehaviour
{
    public GUIText timerText;
    
    ...
}

You can then replace all GetComponent calls with timerText. To set this variable, simply drag and drop the GameObject that contains the GUIText component onto the Timer Text field in the Editor.

The GetComponent method, requires you, that the Timer script is attached to the same GameObject that contains the GUIText, or it is called on a GameObject on which has GUIText attached. It is also recommended, to use GetComponent just once, in the Start function of your Timer class, and store it in a variable.

Source Link
user000user
  • 924
  • 2
  • 7
  • 16

There are basically two ways to access the GUIText.

One way would be a public reference in the class or, like you already did, GetComponent.

The advantage of using a public reference is, that you do not have to attach the timer on the GameObject with the GUIText component. You just declare the variable like so:

public class Timer : MonoBehaviour
{
    public GUIText timerText;
    
    ...
}

You can then replace all GetComponent calls with timerText. To set this variable, simply drag and drop the GameObject that contains the GUIText component onto the Timer Text field in the Editor.

The GetComponent method, requires you, that the Timer script is attached to the same GameObject that contains the GUIText. It is also recommended, to use GetComponent just once, in the Start function of your Timer class, and store it in a variable.