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.
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 () {
}
}
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?
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 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