How to set a GameObject's object in code?

I’m learning to code and I want to set GameObject’s value in code instead of dragging the object over to it in the editor. I looked at other peoples questions but they didn’t work.

private GameObject brick = GameObject.Find(“brick(Clone)”);

They said that, that would set the GameObject’s value in code but when I test it, it says NullReferanceException. When I do it by settings it in the editor I don’t get this error. What am I doing wrong?

Initialization your such variable in function Awake() or Start():

 private GameObject brick = null;

 void Start() {
  //Here initialization
  brick = GameObject.Find("brick(Clone)");
 }

I hope that it will help you.

If you need a reference to a clone its sometimes better to keep it from when you instantiate it.

GameObject brick = Instantiate(brickPrefab) as GameObject;