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 Ochreous · Jun 29, 2013 at 09:05 PM · c#transformarray

C# Children and SubChildren Null Reference Exception

Hi everyone, in one of my scripts I'm getting a null reference exception and I'm trying to figure out why. The console says it's coming from line 9. I have two Transform Arrays someTransformArray1 and someTransformArray2. Both of them have children and subchildren. I want to make it so someTranformArray1's subchildren's sharedMesh equals someTranformArray2's subchildren's sharedMesh. Any idea what is causing this null reference exception?

 public Transform[] someTransformArray1;
 public Transform[] someTransformArray2;
 
 void Awake(){
     foreach (Transform child1 in someTransformArray1)
     foreach (Transform child2 in someTransformArray2)
     foreach (Transform subchild1 in child1)
     foreach (Transform subchild2 in child2)
     subchild1.GetComponent<MeshFilter>().sharedMesh = subchild2.GetComponent<MeshFilter>().sharedMesh;
 }
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 SinisterRainbow · Jun 29, 2013 at 09:09 PM 0
Share

is this all the code? you should just post your code for better answers. childCurrent is not assigned it's telling you.

avatar image Ochreous · Jun 29, 2013 at 09:33 PM 0
Share

That was a mistake in my code. I fixed the name to child1 like it's suppose to be.

1 Reply

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

Answer by Em3rgency · Jun 29, 2013 at 09:10 PM

Add an

 if(subchild1 != null && subchild2 != null)

right above line 9, and you will be dandy. (It wont change the fact that this does not find any subchild1/2 in childCurrent or childOriginal, but it will get rid of that error)

Also, you seem to be running a cycle in a cycle in a cycle in a cycle. Are you SURE that's what you wan't to be doing?

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 Ochreous · Jun 29, 2013 at 09:16 PM 0
Share

I need to access children of children so yes. I do think this way of finding them is rather redundant. Do you know an easier and more efficient way of doing this?

avatar image Em3rgency · Jun 29, 2013 at 09:22 PM 0
Share

Well, no, that's about right, but children of children would only get you 2 levels (1 loop for the children of the object, second loop inside that for the children of children). Not full blown 4. :)

avatar image Ochreous · Jun 29, 2013 at 09:30 PM 0
Share

I'm still getting the null reference Exception.

 public Transform[] someTransformArray1;
 public Transform[] someTransformArray2;
  
 void Awake(){
 foreach (Transform child1 in someTransformArray1)
 foreach (Transform child2 in someTransformArray2)
 foreach (Transform subchild1 in child1)
 foreach (Transform subchild2 in child2)
 if(subchild1 != null && subchild2 != null)
 subchild1.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh = subchild2.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
 }
avatar image Em3rgency · Jun 29, 2013 at 09:39 PM 0
Share

Ok wait, do you create the someTransformArray1 and someTransformArray2 anywhere? Just declaring it like that is not enough.

avatar image Ochreous · Jun 29, 2013 at 11:17 PM 0
Share

I found what was causing the Null Reference exception. I needed to check if subchild1 and subchild2s' $$anonymous$$eshFilters were null not the subchildren themselves.

 if(subchild1.GetComponent<$$anonymous$$eshFilter>() != null && subchild2.GetComponent<$$anonymous$$eshFilter>() != null)

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

17 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

Related Questions

C# SetActive GameObject Array 2 Answers

C# Need Help Refining Ricochet Script 1 Answer

C# locking Main Camera's Rotations 1 Answer

C# Function Transform to Gameobject Convert 1 Answer

C# Specifying which Children to add to the Array 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