"best" Referenze to a GameObject?

Hello Com,

i have a small question: What is the most efficient way to get a reference to a game object?
I am asking, because in many posts i read that GameObject.Find(“GameObjectName”) is slow.

My code:

    [SerializeField] private GameObject obj;
    // Use this for initialization
    void Start () {
        obj = GameObject.Find("box1");
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            // Get movement of the finger since last frame
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

            print(touchDeltaPosition);

            //move a object across XY plane

            

        }

rgds,

GreenTee

Use public gameobject instead of private and just set it yourself in unity. That’s what I do and I can’t say that it’s slow because it has already been defined and doesn’t need to “search” for it.

Not sure if I understand you correctly, why did you make your object private?

public GameObject box;
// assign the box in inspector

void Start () {

box.DoSomething();

}