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 /
This question was closed Sep 07, 2014 at 06:26 PM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Kleptomaniac · Mar 12, 2012 at 10:19 AM · transformpragma strictdynamic typing

Pragma Strict - Implicit Downcast from 'Object' to 'UnityEngine.Transform'

Hey there, I've just been taking to my scripts with #pragma strict, and overall they've done pretty well in terms of an absence of instances of dynamic typing. There is one instance which is boggling me though, and that is this line:

 for(var child : Transform in transform) {
     child.gameObject.layer = 8;
 }

I am getting the error BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Transform'. My first thoughts were, isn't child being typecasted to a Transform by using var child : Transform? But now I'm not so sure. I've tried using as Transform in various places but to no avail.

What is the correct way of typecasting this?

Thanks, Klep

Comment
Add comment · Show 1
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 cregox · Dec 12, 2014 at 10:08 AM 0
Share

related: http://answers.unity3d.com/questions/566864/implicit-downcast-warning-1.html

5 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Mar 12, 2012 at 10:42 AM

That's fine. It's just a warning, not an error, in case you didn't mean to do that, but I don't think there's a way to avoid it when doing "for (x in transform)". You can use #pragma downcast to suppress the warning.

Comment
Add comment · Show 8 · 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 Kleptomaniac · Mar 12, 2012 at 10:46 AM 0
Share

Ahk, thanks a lot Eric. ;)

avatar image Benproductions1 · Jul 11, 2013 at 01:36 AM 0
Share

Or you could explicitly cast it

avatar image Eric5h5 · Jul 11, 2013 at 01:43 AM 0
Share

It is being explicitly cast; read the question again and look at the code.

avatar image Benproductions1 · Jul 11, 2013 at 02:04 AM 0
Share

No it's not!
He needs to cast transform to Transform[]. That gets rid of the warning without ignoring it

avatar image Eric5h5 · Jul 11, 2013 at 02:13 AM 0
Share

You can't cast transform to Transform[]. The code might compile but it wouldn't actually work at runtime.

Show more comments
avatar image
4

Answer by devGuillaume · Apr 17, 2012 at 03:30 PM

AndyZ has a workaround:

 for(child in transform)
 {
   var typedChild : Transform = child as Transform;
   typedChild.gameobject.layer = 8;
 }

I don't know if there is any downside to it 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
1

Answer by elegize · Jan 09, 2014 at 02:24 PM

What works for me:

 for (var i = 0; i < transform.childCount; i++)
 {
   transform.GetChild(i).gameObject.layer = 8;
 }
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
-1

Answer by Eilijah · Oct 04, 2013 at 11:09 AM

 var children : Component[] = transform.GetComponents(Transform); 
 for (var child : Component in children) {
   var typedChild : Transform = child as Transform;
   typedChild.gameobject.layer = 8;
 }
Comment
Add comment · Show 1 · 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 Benproductions1 · Oct 04, 2013 at 12:13 PM 0
Share

Please read the question before posting, and also make sure your code actually fixes the OP's problem. Right now you're not even accessing the transforms children. transform.GetComponents(Transform) is the same as writing [transform].

avatar image
-1

Answer by hyperback · Sep 07, 2014 at 06:11 PM

 var children : Component[] = transform.GetComponents(Transform);
 for (var child : Component in children) {
 var typedChild : Transform = child;
 typedChild.gameobject.layer = 8;
 }

this one will work ^^

Comment
Add comment · Show 1 · 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 Eric5h5 · Sep 07, 2014 at 06:19 PM 0
Share

It won't work...`for (t in transform)` iterates through all children, but GetComponents(Transform) does not get any children. devGuillaume already posted the best answer.

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

Implicit downcast and ArrayList 1 Answer

Implicit Downcast Warning and Converting Arrays 2 Answers

How to set a Transform variable with code (C#) 3 Answers

Perpendicular forward :-) 0 Answers

Third person Camera stop it from going lower than the terrian 0 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