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 BKXZ234 · Oct 26, 2012 at 04:03 AM · getcomponents

how do i fill an array with an objects children?

im struggling to get running code that will take all the children of an object and change a variable

example code:

 var master : Transform;// has a few wheelcollider children
     
 var power : float = 1.0;
 
 private var wheelsArray : Component[];
     
 function start() {
 wheelsArray = master.GetComponentsInChildren(WheelCollider) ;
 }
     
 function Update () {
     
 for(var wheel : WheelCollider in wheelsArray){
 wheel.motorTorque = power ;
 };
     
 }

produces a NullReferenceException

cutting out the array stuff and executing

 for(var wheel : WheelCollider in master){
 wheel.motorTorque = power ;
 };

produces MissingReferenceException: The variable master of 'testScript' doesn't exist anymore.

setting the master vars type as a Component or GameObject causes slightly different errors like ApplicationException: Argument is not enumerable

what am i misunderstanding? why would the variable cease to exist?

Comment
Add comment · Show 2
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 whydoidoit · Oct 26, 2012 at 06:23 AM 0
Share

To start with your Start function is called start and isn't being called - it needs that capital S

avatar image MountDoomTeam · Oct 26, 2012 at 06:39 AM 0
Share

it's pretty easy code, have a look at lots of examples from the questions and you will figure it out-you can print the component transform of different elements of the array to make sure they are filled,and you can do for(arrwheel in arrwheels){

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dscroggi · Oct 26, 2012 at 06:52 AM

Try setting wheelsArray to an array of WheelCollider[]

and call GetComponentsInChildren(WheelCollider) as WheelCollider[]

The computer forgets it's a WheelCollider when it downcasts to Component. It's thinking, 'Wait a minute, there's no WheelColliders in this array, only Components!'

Alternatively I think you could cast it back up to a WheelCollider before using it in the foreach loop.

Hope that helps.

Comment
Add comment · Show 2 · 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 BKXZ234 · Oct 26, 2012 at 07:10 PM 0
Share

turns out the first snippet of code i wrote works in a fresh scene, also found that not specifying wheelsarray type mysterious works around all the errors

avatar image BKXZ234 · Oct 26, 2012 at 07:12 PM 0
Share

thanks for the explanation of the casting errors though, ill keep that in $$anonymous$$d

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

12 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

Related Questions

GetComponents Access to Inherited Class 1 Answer

Check If GameObject Has Render Component 2 Answers

Implicit type GetComponent call 0 Answers

How to look for a game object and a change some components on that object? 1 Answer

Retrieving a variable from ALL scripts on a Game 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