Accessing variable from another gameobject does not work

I`ve tryed different methods, but none of them worked for some reason. What am i doing wrong?

Script 1:

public class FloorProtector : MonoBehaviour {			
GameObject temp = GameObject.Find("Cube");
void Start() {		
	BoxBehavior script = temp.GetComponent<BoxBehavior>();		
	script.isColliding = true;//testing if it works or not
}

Script 2:

public class BoxBehavior : MonoBehaviour
{	
public bool isColliding = false;

I think the problem is that you tried to find the Cube in the variables. You shoud allways use the GameObject.Find() in the Start() and in the Update()

so the code is:

public class FloorProtector : MonoBehaviour {            
 GameObject temp;
 void Start() {        
     temp = GameObject.Find("Cube");
     BoxBehavior script = temp.GetComponent<BoxBehavior>();        
     script.isColliding = true;//testing if it works or not
 }

Try to make refference in the Editor:

 public class FloorProtector : MonoBehaviour {     
	[SerializeField]
	BoxBehavior script;		
	void Start() {             
	  script.isColliding = true;
	}
  }

If you really want to use Find, make shure that:

  • there is only 1 GameObject named
    “Cube” (because Find will return first match);
  • BoxBehavior is on the “Cube” (not its child or parent).