enable and disable boxcollider (whats wrong with my script?)

Hello,I got a problem in my script. But actually I don’t even know what I did wrong. I hope you can help me out.

This script should find the gameobject and disable or enable the “BoxCollider2D” of it.
I did this so that the boxcolliders wouldn’t overlay eachother.
The first 5 objects you see in the script have their boxcollider2D disabled, and have yet to be enabled. This happens when you click on the Collider where this script has been assigned to.

This is the error i get :
NullReferenceException: Object reference not set to an instance of an object
OnStart3.OnMouseDown () (at Assets/OnStart3.js:4)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

function OnMouseDown() {
    Debug.Log("click");
    
    GameObject.Find("back2home").GetComponent(BoxCollider2D).enabled=true;
    GameObject.Find("world1").GetComponent(BoxCollider2D).enabled=true;
    GameObject.Find("world2").GetComponent(BoxCollider2D).enabled=true;
    GameObject.Find("world3").GetComponent(BoxCollider2D).enabled=true;
    GameObject.Find("world4").GetComponent(BoxCollider2D).enabled=true;
    
    GameObject.Find("Beginscreen").GetCompenent(SpriteRenderer).enabled=false;
    GameObject.Find("Optionmenu").GetComponent(BoxCollider2D).enabled=false;
    GameObject.Find("ToStart").GetComponent(BoxCollider2D).enabled=false;
    GameObject.Find("Twitterredirect").GetComponent(BoxCollider2D).enabled=false;
    }

Thanks for reading. I hope you can help me out.

EDIT : Fixed the problem. There is nothing wrong with the script. Rather Unity is being weird as always. Seems like the problem fixed itself (thanks for all the help)

Disabled objects are not found by GameObject.Find funcion, therefore your first five objects won’t be found if they’re disabled in the scene. If they are not found, the result of the Find function will be null and the GetComponent will give you that error (since there is no GameObject to apply the GetComponent function to).
That’s one common mistake.

The answer is simple either:

  • There’s no GameObject called “back2home”
  • Or the GameObject is disabled
  • Or this gameobject doesn’t have a BoxCollider2D

You don’t do any checks if the object is actually there and if it has a BoxCollider2D. Also in line 10 you try to “enable” the GameObject itself which doesn’t work since a GameObhect doesn’t have an enabled property.

This should help to pin down the problem:

function EnableBoxCollider(objName : string, enable : boolean)
{
    var obj = GameObject.Find(objName);
    if (obj == null)
    {
        Debug.LogError("EnableBoxCollider: There's no active object named '"+objName+"'");
        return
    }
    var col = obj.GetComponent(BoxCollider2D);
    if (col == null)
    {
        Debug.LogError("EnableBoxCollider: The object named '"+objName+"' doesn't have a BoxCollider2D");
        return
    }
    col.enabled = enable;
}

function OnMouseDown() {
    Debug.Log("click");
    
    EnableBoxCollider("back2home", true);
    EnableBoxCollider("world1"), true);
    EnableBoxCollider("world2"), true);
    EnableBoxCollider("world3"), true);
    EnableBoxCollider("world4"), true);
    
    EnableBoxCollider("Beginscreen", false);
    EnableBoxCollider("Optionmenu", false);
    EnableBoxCollider("ToStart", false);
    EnableBoxCollider("Twitterredirect", false);
}