- Home /
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)
This error can be thrown if the system is unable to find one of the game objects. Please doublecheck if all objects are in the scene and spelled exactly like in the script (it is case sensitive!).
Answer by Andres-Fernandez · Apr 28, 2014 at 09:22 AM
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.
And remember that you enable an object by using function SetActive ins$$anonymous$$d of enabled property (enabled property is used for components). Just for line 10.
$$anonymous$$y fault. I just realized it. (the 5 gameobjects are indeed enabled in the scene). It also does enable the boxcolliders for these 5 objects but it doesn't disable the other 3 colliders.
Then, as other answers suggest, check for the spelling of the objects.
And I would add just one thing: If those objects are not dinamically instantiated, set them as public variables in the script and link them in editor view. That way you won't have to worry about names.
Answer by Bunny83 · Apr 28, 2014 at 09:26 AM
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);
}
Beginscreen is actually a sprite and should be disabled on mouseclick. But Yeah thanks for the help :). I will try it out
edit : I guess I can also just disable the sprite render