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
0
Question by Kid Roleplay · Dec 10, 2012 at 05:53 AM · soundcomponentchildrenenablebox collider

How to select BoxColliders of children and disable them?

Hello.

I'm trying to make my project recognize the Box Colliders of all of its children and enable them during the specific steps of a walking animation for one second, then disable them again, but Unity cannot find what I'm asking of it (NullReferenceException). Granted, the sound works perfectly fine during the trigger, unless the player/ camera happens to be facing away from what's walking; but it cannot find the colliders at all.

Here's the erroneous code I have. It's simple, but I'm new as well:

var audioVolume = 1.0;
var collisionSoundEffect : AudioClip;

function playSound() {

 audio.volume = audioVolume;
 audio.clip = collisionSoundEffect;
 audio.Play();
 
 gameObject.GetComponentsInChildren(BoxCollider).collider.enabled = true;
 yield WaitForSeconds(1);
 gameObject.GetComponentsInChildren(BoxCollider).collider.enabled = false;
 }
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 Kid Roleplay · Dec 13, 2012 at 05:12 AM 0
Share

I take it there's no way to make this work?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by idunlop_oefun · Dec 11, 2012 at 01:31 AM

I don't fully understand javascript syntax (I use c#), however

 GetComponentsInChildren

returns an array of BoxCollider(s). So ideally you want to store that array in a variable then loop through each BoxCollider and enable / disable it.

I'm guessing the exception is due to you trying to access the 'collider' property on an array (which doesn't exist).

One other thought is that your collider is not on the child, so perhaps you just want to use GetComponent instead of GetComponentsInChildren.

Comment
Add comment · Show 5 · 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 Kid Roleplay · Dec 11, 2012 at 03:04 AM 0
Share

Well, the script is on the highest parent object, but the box colliders (there's four of them) are much deeper within. So I was trying to have that script, when it triggers while the walking animation is playing, to both play a sound effect and also enable those box colliders for one second and then disable them. Unfortunately, I don't really know how to make arrays or variables.

avatar image idunlop_oefun · Dec 13, 2012 at 07:55 AM 0
Share

There are likely dozens of resources on this site and others that will tell you how to declare arrays using javascript.

The syntax in c# is:

GameObject [] objs = gameObject.GetGomponentsInChildren(); foreach(GameObject obj in objs) { if (obj.collider != null) { obj.collider.enabled = true; } }

avatar image Kid Roleplay · Dec 14, 2012 at 12:33 PM 0
Share

Considering how much of a n00b I am with Unity... is it possible to make C# scripts? If not, I'd hafta find a way to make that into Javascript. x.x

avatar image Kid Roleplay · Dec 22, 2012 at 03:37 AM 0
Share

I get this error when using that:

Assets/HitSwitch.cs(8,41): error CS1061: Type UnityEngine.GameObject' does not contain a definition for GetGomponentsInChildren' and no extension method GetGomponentsInChildren' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

avatar image idunlop_oefun · Feb 13, 2013 at 04:25 AM 0
Share
         BoxCollider [] colls = mygameobject.GetComponentsInChildren< BoxCollider>();
 

This is an issue with the comment / html. See the missing part? Remove the 'space' character immediately after the left arrow.

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

10 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

Related Questions

How to enable children of child game object? 1 Answer

How to disable/enable SSAOEffect or other specific Components? 1 Answer

C# Find component InChildren 3 Answers

Add script to many multiple children 1 Answer

disable a component that script is attached too 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