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 ina · May 09, 2011 at 01:28 AM · transformobjectconversiontypecastingpragmastrict

pragma strict convert object error

I get the following error below for #pragma strict - "Cannot convert 'Object' to 'UnityEngine.Transform'." How do you correctly typecast this?

o is an GameObject - defined in the function argument

function fred(o: GameObject){
  for(var child : Transform in o.transform){
    child.renderer.material = m;
  }
}   
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 Jessy · May 09, 2011 at 12:07 PM 0
Share

There are no problems with your function. Try it again in a new file/project. If you have C# scripts in your project, too, the error won't go away after you fix it, until you restart Unity, unless your folders are set up properly for mixing languages.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · May 09, 2011 at 02:30 AM

You need to tell the compiler what o is. I assume you just have

var o;

somewhere.

You also don't need to make the transform a transform (with as). It already is one. If o can be a transform, then you can do this:

for (var child : Transform in o) child.renderer.material = m;

Otherwise, this:

for (var child : Transform in o.transform) child.renderer.material = m;
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 ina · May 09, 2011 at 04:22 AM 0
Share

i had var o:GameObject; or rather, a function with that defined as an argument ... and i was using the latter of your snippets when I got the error above

avatar image
0

Answer by loramaru · May 09, 2011 at 06:51 AM

I wish I could say why that doesn't work under strict (perhaps someone more knowledgeable will come along with that answer), however this will get your code working...

for(var childObj : Object in o.transform){
    var child : Transform = childObj as Transform;
    child.renderer.material = m;
}
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 Jessy · May 09, 2011 at 12:04 PM 0
Share

That doesn't change the above, other than maybe being slower (depending on the compiler).

avatar image loramaru · May 10, 2011 at 12:38 AM 0
Share

I doesn't change it functionally but it addresses the error induced by the strict pragma. I'm not sure why the iterator on GameObject.transform seems to return Objects rather than Transforms; but that is the behavior that ina was seeing and it's what I see in Unity as well. Perhaps this is intentional, a quirk of $$anonymous$$ono that I don't know yet, or even a Unity bug; regardless the explicit cast appears needed with the strict pragma.

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

No one has followed this question yet.

Related Questions

object not set to instance..........again. 2 Answers

pragma strict issues with iOS 1 Answer

Global Variable[Solved] 1 Answer

error CS0266: Cannot implicitly convert type `object' to `UnityEngine.Vector3'. An explicit conversion exists (are you missing a cast?) 1 Answer

Move Object to location of Trigger? 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