• 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 easilyBaffled · Aug 09, 2012 at 01:40 AM · casting#pragma strict

for(...in...) causing problems

I'm trying to iron out some errors, and I admit I maybe a little too tired at the moment, but here's the thing. i am trying to iterate through a set of objects, move each one, and create a new one at the end. But I have been getting a lot of type errors, I've gotten most of them(I think) But this one eludes me. I get "InvalidCastException: Cannot cast from source type to destination type." on the for(…in…). Does anyone know why? I'm not ever sure which object its being called on.

 #pragma strict
 
 var Strait : Transform;
 var childPos;
 var newTail : Transform;
 function Start () {
  for (var child : GameObject in gameObject){
  childPos = child.transform.position;
  child.transform.position.z--;
  if(child.transform.position.z < -1){
  Destroy(child);
  }
  if(child.tag == "Tail"){
  
  newTail = Instantiate(Strait, child.transform.position,  Quaternion.identity);
  newTail.tag = "Tail";
  newTail.parent = transform;
  child.tag = "Untagged";
  }
  }
 }


With out the #pragma strict, I get "ApplicationException: Argument is not enumerable (does not implement System.Collections.IEnumerable). Boo.Lang.Runtime.RuntimeServices.GetEnumerable (System.Object enumerable) UnityScript.Lang.UnityRuntimeServices.GetEnumerator (System.Object obj) Tube.Start () (at Assets/Tube.js:7)" And I really have no idea what that means.

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
-1

Answer by Muuskii · Aug 09, 2012 at 01:49 AM

gameObject is actually a shortcut for GetComponent( GameObject )

So what you're actually doing with:

  for (var child : GameObject in gameObject){

Is "Find all the GameObjects in the list known as me"

Unity's like: "What bro? What?"

Try:

 for (var child : GameObject in GameObject){

Notice the capitalization.

Hope this helps!

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 Eric5h5 · Aug 09, 2012 at 02:59 AM 0
Share

GameObject is not a component, therefore GetComponent(GameObject) will never work, and it's not what gameObject does (it simply refers to the GameObject the script is attached to). "for (var child : GameObject in GameObject){" doesn't make sense either...GameObject is a class, not a collection, so it's not something you can iterate over.

avatar image Muuskii · Aug 09, 2012 at 03:51 AM 0
Share

I. . . Have no idea what I was thinking when I typed that. . .

avatar image
1

Answer by whydoidoit · Aug 09, 2012 at 01:42 AM

GameObject doesn't store a collection of children - that would be transfrom:

   for(var child : Transform in transform) {

   }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Problems with lightmapping 1 Answer

Cannot cast from source type to destination type in C# 1 Answer

Parseing JTokens 0 Answers

Interactions between scripts when using #pragma strict or on Android 1 Answer

InvalidCastException with .net datatypes but not with UnityEngine.Object 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