Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Jamiemon · Jul 04, 2017 at 08:15 AM · collidersdisableenable

How do you enable/disable colliders of specific child gameobjects? {java}

I'm running a test where a gameObject is changing sprites from a box to a skateboard to test changing colliders. So far, I've created two empty child gameobjects with colliders corresponding to the two sprites' shapes {one box collider, & one polygon collider}. What I'm trying to do is enable one collider & disable the other whenever I want to switch between the two, but none of the answers I've found so far have worked. Closest I've gotten with the fewest errors is this:

 gameObject.Find("boxCol").GetComponent.BoxCollider().Enabled = true;
         gameObject.Find("skakteCol").GetComponent.PolygonCollider().Enabled = false;

Any help is much appreciated

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SohailBukhari · Jul 04, 2017 at 09:14 AM

you are not using correct syntax for finding gameObject. The GameObject which you are trying to find make sure is active in hierarchy then disable. For more information go to link https://docs.unity3d.com/ScriptReference/GameObject.Find.html

 #pragma strict
     function Start () {
             var BoxCol:
             BoxCollider = gameObject.Find("boxCol").gameObject.GetComponent. < BoxCollider > ();
         BoxCol.enabled  = false;
     }


Comment
Add comment · Show 7 · 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 Jamiemon · Jul 04, 2017 at 06:45 PM 0
Share

It's giving me an error message "$$anonymous$$ identifier: 'boxCol'." as well as with skateCol Is it the same syntax for the PolygonCollider2D? Here's the whole script:

 var longboard : Sprite;
 var testBo : Sprite;
 
 function Start () {
     var boxCol:
     BoxCollider = gameObject.Find("boxCol").gameObject.GetComponent. <  BoxCollider > ();
     var skateCol:
     PolygonCollider2D = gameObject.Find("skateCol").gameObject.GetComponent. < PolygonCollider2D > ();
 
 }
 
 function Update () {
     if(GameObject.Find("wWolf").transform.position.x > 0){
         transform.position.x = 20;
         GetComponent(SpriteRenderer).sprite = longboard;
         boxCol.enabled = false;
         skateCol.enabled = true;
     }else{
         transform.position.x = -3;
         GetComponent(SpriteRenderer).sprite = testBo;
         boxCol.enabled = true;
         skateCol.enabled = false;
     }
     }
avatar image Vollmondum Jamiemon · Jul 04, 2017 at 06:57 PM 0
Share
 Variables should be declared outside functions.
 
 var longboard : Sprite;
 var testBo: Sprite;
 var boxCol: BoxCollider;
 var skateCol: PolygonCollider2D;
 
 function Start ()
 {
      boxCol = gameObject.Find("boxCol").GetComponent.<BoxCollider>();
      skateCol = gameObject.Find("skateCol").GetComponent.<PolygonCollider2D>();
  }
avatar image Jamiemon Vollmondum · Jul 04, 2017 at 07:24 PM 0
Share

That got rid of the errors, thanks But is seems that both colliders are both enabled &

 function Update () {
     if(GameObject.Find("wWolf").transform.position.x > 0){
         transform.position.x = 20;
         GetComponent(SpriteRenderer).sprite = longboard;
         boxCol.enabled = false;
         skateCol.enabled = true;
     }else{
         transform.position.x = -3;
         GetComponent(SpriteRenderer).sprite = testBo;
         boxCol.enabled = true;
         skateCol.enabled = false;
     }
     }

is changing the sprites & the x position, but both colliders are stuck enabled.

Show more comments
avatar image SohailBukhari · Jul 05, 2017 at 09:05 AM 0
Share

Hmm.... i think you pasted my code in the c# script ins$$anonymous$$d of JavaScript file.$$anonymous$$ake sure you created JavaScript script.

avatar image SohailBukhari SohailBukhari · Jul 05, 2017 at 09:07 AM 0
Share

i pasted the link of unity documentation you can see more details from the unity panel, this is basic,try to find first at unity documentation about syntax errors, if your problem not solved then ask question.

avatar image
0

Answer by Vollmondum · Jul 04, 2017 at 10:57 AM

You're dong everything right, just need to add a condition for a switch:

 var usingSkateboard: boolean;
 var switchCollider: boolean;
 
 if(switchCollider)
 {
           switchCollider = false;
           usingSkateboard = !usingSkateboard;
 }
 if(usingSkateboard)
 {
           gameObject.Find("boxCol").GetComponent.<BoxCollider>().enabled = true;
           gameObject.Find("skakteCol").GetComponent.<PolygonCollider>().enabled = false;
 }
 else if(!usingSkateboard)
 {
           gameObject.Find("boxCol").GetComponent.<BoxCollider>().enabled = false;
           gameObject.Find("skakteCol").GetComponent.<PolygonCollider>().enabled = true;
 }

And then the only thing you need is to tick that switchCollider thing.

Comment
Add comment · 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

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

67 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 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 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

image not enabling 0 Answers

Disabling collsions between MeshColliders and bullets 2 Answers

Enable/Disable Game Object With GUI Button 1 Answer

Enabling loop and disabling loop 1 Answer

Enable/Disable a Static object 1 Answer


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