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 lakehaze · Mar 20, 2011 at 11:25 AM · prefabparentchildnullfind

Access children using Find() returns 'null'?

I'm trying to access the children in a prefab. Internet says that I should use: var child = transform.Find("child");

I got it to work once (that file is long gone), but since then it always returns a null. My current scene includes the following hierarchy: vehRigA -axleFront -axleRear

But the following code,

var axleF = transform.Find("axleFront");
function update(){
    print(axleF); // returns: null
}

shows that the Find() is not finding.

What am I doing wrong? Should I be using GameObject.Find(),(tried it and got an error) or another kind of Find?

(why can't we access a child by "parent.child"? I thought that was the definition of OO programing?)

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 Statement · Mar 20, 2011 at 11:20 PM 0
Share

parent.child, I guess you mean parent.Find('someName'), which would be finding a sibling of the object, not a child. And it does work in case you have a parent, and its failure to perform what you want has nothing to do with OO program$$anonymous$$g. It's dependent on your hierarchy setup. Does the object your script sits on have a parent node? If it doesn't it'll throw a null reference exception since you'd be calling Find on a null object. (Parent return null if it doesn't have a parent. It doesn't have a parent if, well, it isn't parented to one)

avatar image · Mar 22, 2011 at 07:12 AM 1
Share

I've had this stuck in my head since reading your variable name: http://www.youtube.com/watch?v=IG8EdbrSVtc

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by efge · Mar 20, 2011 at 12:18 PM

Is this script attached to the object "vehRigA"? If not you should use the whole path to the child
(vehRigA may not have a parent in the hierarchy view):

var axleF : Transform;

function Start () { axleF = transform.Find("/vehRigA/axleFront"); //use the full path from root }

function Update(){ ... }

(or use GameObject.Find() which finds every object and child without the path)

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 lakehaze · Mar 20, 2011 at 01:06 PM 0
Share

Yes, the script is attached to vehRigA. I went ahead and tried anyway (copied from unity scene): var axleF:Transform; function start(){ axleF = transform.Find("vehRigA/axleFront"); } function Update () { print(axleF); //null! } I adapted my code to be like yours to eli$$anonymous$$ate any other errors; previously, I wasn't running the Find() in the start() function. Sadly, it still isn't working.

avatar image efge · Mar 20, 2011 at 04:36 PM 0
Share

Tested and edited my answer.

avatar image efge · Mar 20, 2011 at 04:40 PM 0
Share

From the reference: "For performance reasons it is recommended to not use this function every frame Ins$$anonymous$$d cache the result in a member variable at startup."

avatar image
1

Answer by invadererik · May 20, 2013 at 04:59 AM

note capitalization: Start vs start (its even colored purple in one versus the other )

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 lakehaze · Mar 22, 2011 at 03:07 AM

Okay, seems I'm learning a scripting syntax lesson: Spaces are important; you must put spaces everywhere or it does not work.

//does not work var axleF:Transform;

function start(){ axleF = transform.Find("/vehRigA/axleFront"); } function Update () { print(axleF); }

//works var axleF : Transform;

function Start () { axleF = transform.Find("/vehRigA/axleFront"); }

function Update(){ print(axleF); }

Thanks for your help :)

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 Mike 3 · Mar 22, 2011 at 06:53 AM 0
Share

That's just bizarre :S

avatar image lakehaze · Mar 30, 2011 at 03:16 PM 0
Share

Thank you for validating the bizarre-ness of this. Over time, this post has festered in my brain. I come back now and again, and just stare at the issue. Seems like the devs might be curious about what's causing this.

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

1 Person is following this question.

avatar image

Related Questions

Make a simple tree 1 Answer

Finding child problems.. 1 Answer

Can I limit a .Find to the Parents and Child and not the Scene? 3 Answers

find ALL children of a parent 1 Answer

How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene? 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