-1
\$\begingroup\$

I want to write a text in game scene... but when I type...

void Update() {
     GUIText.t|

Then the code completion hints do not offer to complete this "t..." to GUIText.Text.

Why is that? And how can I assign text to display in Unity 5.6.4 (64 bit)?

\$\endgroup\$
2
  • \$\begingroup\$ More importantly, have you typed what you intended to type, and then tried to run it? Sometimes MonoDevelop will not show certain things that are indeed there (I think this is an issue with the assembly). \$\endgroup\$
    – Engineer
    Commented Feb 18, 2018 at 17:42
  • \$\begingroup\$ yes i typed what i want and then tried to run but the error say : " An object reference is required to access non-static member `UnityEngine.GUIText.text' " \$\endgroup\$
    – yatayama
    Commented Feb 18, 2018 at 17:51

1 Answer 1

1
\$\begingroup\$

Because you are trying to reference a static member that doesn't exist.

We do not access like this:

GUIText.text = "whatever"; //trying to reference a static member that doesn't exist

but rather like this:

    GUIText guiText = new GUIText(); //have now created an "object" A.K.A. "instance"
//...or otherwise you can drag an "object reference" in via Unity's inspector.
    guiText.text = "whatever"; //now have the "object reference" on which "text" exists

Also remember that programming languages are nearly always case-sensitive, so you must be careful for example to use GUIText and not GUItext or GuiText. The names must be precise.

\$\endgroup\$
4
  • \$\begingroup\$ //...or otherwise you can drag an "object reference" in via Unity's inspector. yes, i'm doing this way but then when i want to write GUIText.Text (in Update() ), there is no "Text" after GUIText. . \$\endgroup\$
    – yatayama
    Commented Feb 18, 2018 at 18:20
  • 1
    \$\begingroup\$ Note that most components in Unity prefer to be created on a GameObject using AddComponent - trying to create free-floating component instances with new can cause unexpected behaviour. \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2018 at 18:21
  • \$\begingroup\$ from game object i'm clicking on creat empty then from that empty object in hierarchyi'm clicking on add component in inspector then add GUIText, after that i creat c# script and drag it to that empty object , in that c# script ( in update() ) i write GUIText. but after that there is no Text . \$\endgroup\$
    – yatayama
    Commented Feb 18, 2018 at 18:36
  • \$\begingroup\$ @yatayama it sounds like you're forgetting to either: 1) type the field you drag your GameObject into as public GUIText myGuiText or 2) Use var myGuiText = myGuiTextObject.GetComponent<GUIText>(). Either way, you can then write myGuiText.text = ... (note that your access here is via an instance of GUIText, not via the class itself as a static property) \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2018 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .