Question by
SimRuJ · Feb 23, 2021 at 03:48 PM ·
gameobjectscript.runtimereference
Set object reference for script at runtime
I've got a simple script:
public class ObjectClicker : MonoBehaviour {
public TextMeshPro ObjectNameGO;
private void OnMouseDown() {
Debug.Log("Clicked Object '" + this.name + "'");
}
}
"ObjectNameGO" is a TextMeshPro text that'll display the name of a special object, when the player clicks on it. These special objects are loaded at runtime and the above script is also attached at runtime (with AddComponent<ObjectClicker>
). I know that I can find the right "ObjectNameGO" text object with GameObject.Find("ObjectNameGO")
but I'm wondering:
Is there a way to actually set the reference to the TextMeshPro in the editor, like you would do it with scripts that are already attached to a GameObject (either drag or pick from the list)?
If that's possible: Is there a huge difference between assigning the GameObject at runtime with GameObject.Find()
and assigning it to the script before the game is started?
Comment