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 Luca91 · Jun 04, 2013 at 05:02 PM · gameobjecttransform

Problem accessing child GameObjects

Hello, I need to change the value of a variabile from another script. Here is the code I'm using:

 playerPrefab.GetComponentsInChildren(true)[0].allWeapons[0]=(WeaponScript)transform.Find("BallisticKnife");

In other words I need to change the allWeapon[0] value with a gameobject (child of PlayerPrefab) called "BallisticKnife". The type of BallisticKnife is "WeaponScript". The problem is that unity report an error:

error CS0030: Cannot convert type UnityEngine.Transform' to > WeaponScript'

Any help please ? Many thanks !

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Adamcbrz · Jun 04, 2013 at 05:09 PM

If i understand your question correctly you need to not use get component once you find the object.

 transform.Find("BallisticKnife").GetComponent<WeaponScript>();
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 Jessy · Jun 04, 2013 at 05:10 PM

Transform.Find returns a transform. You don't cast a Transform to the type of your script. Get the Component you need attached to the transform.

GetComponentsInChildren(true) doesn't mean anything. Your code doesn't convey meaning. You should try rewriting it again to do so. We can help.

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 Luca91 · Jun 04, 2013 at 06:39 PM 0
Share

GetComponentsInChildren(true) works because in Unity 4.0 the GetComponentsInChildren() function is a bit different than the one in 3.x .. Infact "playerPrefab.GetComponentsInChildren(true)[0].allWeapons[0]" works perfectly ...

avatar image Jessy · Jun 04, 2013 at 06:45 PM 0
Share

Voted down for lying or not testing your code. GetComponentsInChildren(true) doesn't work, because it conveys no meaning. You're not telling it what component to look for. Assigning to the first element of that array conveys no meaning. Convey meaning.

avatar image Luca91 · Jun 04, 2013 at 06:56 PM 0
Share

Jessy, ins$$anonymous$$d of downvoting read the last post here: http://forum.unity3d.com/threads/154093-Strange-NullReferenceException .. Then please stop to reply this question. Thanks.

avatar image Adamcbrz · Jun 04, 2013 at 07:17 PM 1
Share

Sorry Luca91 but you just proved his point (not trying to be rude).

 o.GetComponentsInChildren<ShipNavigator>(true)[0];

This includes a type of ShipNavigator while yours doesn't include a type. But I think you would have received that error once you resolve the transform error. Which I address in my original answer.

avatar image Luca91 · Jun 04, 2013 at 07:27 PM 0
Share

oh God you are right ! In the working line of code I've a type! Well, thanks $$anonymous$$for pointing this out. Also, Jessy sorry it was my fault then ;)

EDIT: the correct code was:

 playerPrefab.GetComponentsInChildren<Weapon$$anonymous$$anager>(true)[0].allWeapons[0];

:)

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

16 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

Related Questions

Deploy object avoiding collisions 0 Answers

How to delay a line of code. 2 Answers

transform.parent can't go with gameObject.find 3 Answers

How to reactivate a gameObject? 3 Answers

Cannot conver UnityEngine.Transform to UnityEngine.GameObject 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