Change specific text C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MoneyBtn : MonoBehaviour {

// Use this for initialization
void Start () {
var cash = 0;
cash = cash + 1;
GameObject.Find(“/Canvas/MoneyTxt”).GetComponent().text= “Hello”;

}

// Update is called once per frame
void Update () {

}
}
It doesn’t work.
Warning:
There are inconsistent line endings in the ‘Assets/MoneyBtn.cs’ script. Some are Mac OS X (UNIX) and some are Windows.
This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands.

GUIText is the wrong component - use Text.

(GUIText, along with GUITexture, is part of the old, old, OLD UI system, as in two complete overhauls ago, and should just not ever be used)

Additional notes:

  1. Post code with code tags to make reading it easier.
  2. Glad you decided to take the C# plunge. :slight_smile: It’s nicer on this side

@StarManta
I changed to text, still doesn’t work, same warning.

The warning means nothing other then some line endings don’t match. Doesn’t hurt your code any. I know in Visual Studios it will prompt and you can just tell it to fix line endings. On Monodevelop, I don’t recall if it prompts when you view the script, but either way, it isn’t going to hurt your code.

If your text isn’t being set, there is another issue.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoneyBtn : MonoBehaviour {
   
 // Use this for initialization
 void Start () {
        var cash = 0;
        cash = cash + 1;
        GameObject.Find("/Canvas/MoneyTxt").GetComponent<Text>().text= "Hello";
        
 }
 
 // Update is called once per frame
 void Update () {
  
 }
}

No errors, just doesn’t work…

The script looks fine to me (I mean, aside from having GO.Find, but… one thing at a time, and that shouldn’t prevent it from working), which makes me suspect it has to do with the setup in the editor. Can you post some screenshots of that?

Make a public Text field, drag the text ui component into it, then reference it directly.

Unless lives are at risk, never use Find().

What object is this script attached to?

You shouldn’t really use gameobject find with string, however, your object you are looking for is “MoneyTxt” not “/Canvas/MoneyTxt”

It allows the full hierarchy “path” according to the docs.

Not that that makes GO.Find good or anything. But, it won’t be the reason this particular thing isn’t working.

Fair enough, never used the function myself. I don’t like the idea that if I change the name of something I would have to remember to do it in every script which looks for it.

Preaching to the choir here :slight_smile: I wrote a whole article on the subject…

But OP just switched from JS to C# (partly on my urging) shortly before asking about this, so I’m willing to take that one step at a time. I remember how stressful things are the first time you use a language before you get anything to work, so I’ll wait until he gets the first thing to work before going into improving habits :slight_smile:

@StarManta
@WarmedxMints_1
Can u guys help me out here?