Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Hexer · Apr 28, 2014 at 09:08 AM · javascriptgameobjectgetcomponentboxcolliderenable

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gnometech · Apr 28, 2014 at 09:26 AM 0
Share

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!).

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andres-Fernandez · Apr 28, 2014 at 09:23 AM 0
Share

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.

avatar image Hexer · Apr 28, 2014 at 09:30 AM 0
Share

$$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.

avatar image Andres-Fernandez · Apr 28, 2014 at 09:36 AM 0
Share

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.

avatar image
1

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hexer · Apr 28, 2014 at 09:32 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to enable/disable Javascript using a C# script? 1 Answer

How to deactivate something when holding down a key? 1 Answer

Turn off CharacterControl Component Through Script? 1 Answer

Best Unity Practices 1 Answer

disable/enable Mouse Look(c#) with a script of js 7 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges