Hello all,
I feel like beeing in an endless loop…
I have a LevelUIManager holding a reference to a text UI object “txt_currentSpeed”.
public class LevelUIManager : MonoBehaviour {
public Text txt_Distance;
…
My first approach was to use the inspector to bind the text object to the public field of my LevelUIManager class.
My LevelUIManager is a prefab in order to use it in every level and with this approach I was facing the problem that the references were not working when reusing this prefab in different scenes and after some research I found out that it is not possible to reference scene objects from a prefab. Okay, that’s a pity, but the solution I found everywhere was “use GameObjects.Find()” to get the reference.
…
Okay, hence, my second approach looks like this:
private GameObject completeLevelUI;
private Text txt_Distance;
private void Start()
{
txt_Distance = GameObject.Find("txt_Distance").GetComponent<Text>();
completeLevelUI = GameObject.Find("LevelComplete");
While getting the txt_Distance reference works fine, getting the LevelComplete reference does not work since it is a panel which I disabled in order to active it when I need it.
Doing some research again brought up lots results which say: “Find() is always the worst method to use to get a reference, use the inspector instead”
…
GREAT, now I am at the beginning again…
That is the point where I want to ask you to explain me, which is a meaningful way to deal with my issues.
- When can / should I use the inspector method
- When can / should I use find()
- What is a good way to get a reference to a disabled object
Thanks in advance!