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 Mariouch · Oct 11, 2012 at 09:00 AM · arrayschildrenfunctionsbasic

Access function from script of all children?

Heya, I have an empty object called "AllTheDoors" that is the parent of every door in the game. Each door has a script called "DoorScript.js", and within that, a function called recieveProperties(). How do I call this function for every child? I wrote this so far:

 var DoorScripts : DoorScript[];
 DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];
 
 for (var child : DoorScript in DoorScripts)
 {
     child.recieveProperties(true, true, true);
 }

The only issue is that I continually receive this error:

NullReferenceException: Object reference not set to an instance of an object

Please help as soon as possible.

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 Fattie · Oct 11, 2012 at 09:05 AM 1
Share

add somethign like Debug.Log doorScripts.length

avatar image always_beta · Oct 11, 2012 at 09:47 AM 0
Share

check exactly which line raises that exception.

4 Replies

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

Answer by Mariouch · Oct 11, 2012 at 09:52 AM

Lol I actually just found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html

Instead of: var DoorScripts : DoorScript[];

I was supposed to put: var DoorScripts : Component[];

I was confused by a previous Unity Question. Bah, my bad. But thanks so much for your help anyway!

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
avatar image
0

Answer by rutter · Oct 11, 2012 at 09:20 AM

You might try moving some of that code into an `Awake()` or `Start()` call, like so:

 var DoorScripts : DoorScript[];
 
 function Start()
 {
     DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];
     for (var child : DoorScript in DoorScripts)
     {
         child.recieveProperties(true, true, true);
     }
 }

If that's not the problem, try to figure out which reference(s) are null: the array itself, or its contents. You could, for example, add `Debug.Log(child)` inside the loop to list each child before operating on it.

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 Mariouch · Oct 11, 2012 at 09:25 AM 0
Share

The code is actually in a function call that is initiated on Start(). I used Debug.Log, and it appears that the entire array itself is null. Perhaps I'm doing something wrong here:

DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];

avatar image rutter · Oct 11, 2012 at 09:34 AM 0
Share

Hmm... the `as` operator will return null, in the event that the typecast fails. Does your `DoorScript` class extend from `$$anonymous$$onoBehaviour`? See here for a little bit more detail: http://wiki.unity3d.com/index.php/Head_First_into_Unity_with_UnityScript#Each_.js_File_Implements_A_Class_.28By_Default.29

avatar image Mariouch · Oct 11, 2012 at 09:40 AM 0
Share

DoorScript is not a class, it's a script. I put "as DoorScript[]" because GetComponentsInChildren() returns a type different from the source.

avatar image rutter · Oct 11, 2012 at 09:47 AM 0
Share

Could we get a peek at the contents of that script?

avatar image rutter · Oct 11, 2012 at 09:59 AM 0
Share

No problem. Glad your stuff's working!

avatar image
0

Answer by Mariouch · Oct 11, 2012 at 11:42 AM

Bah, I was confused by another Unity Question. I found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html

Instead of:

 var DoorScripts : DoorScript[];

I was supposed to put:

 var DoorScripts : Component[];

Moral of the Story: Read the f-ing manual... -.-

But thanks to everyone else who helped, though! :)

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
avatar image
0

Answer by Mariouch · Oct 11, 2012 at 11:42 AM

I actually just found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html

Instead of:

 var DoorScripts : DoorScript[];

I was supposed to put:

 var DoorScripts : Component[];

I was confused by a previous Unity Question. My mistake. Thanks everyone for your help, anyway!

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

11 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

Related Questions

how to access scripts in children 2 Answers

How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation? 1 Answer

Destroy created children of objects 1 Answer

question about how - precisely - looking through children works random or alphabetical. 1 Answer

Inventory system? 3 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