29 Replies
What is the type of
transform
Do you have the stack trace (and line number) where the error ocurred?
line 104
Which line is that in your code block
foreach(GameObject go in transform)
Well, a
Transform
is not a GameObject
from what I seeyea
So that's the issue
found this
Man, unity's docs are hard to read
on a phone
It doesn't document what
GetEnumerator()
returns at all
Period
Can you tell me what transform.GetEnumerator()
returns?
Because their online docs are actually bad enough that I can't find this outI cant find anything on the docs
Yes, that's why I'm asking you to try it in your code and tell me what it returns
k
Like, just do
var x = transform.GetEnumerator();
, then hover over var
and tell me what it says the type isTransform
Screenshot?
I searched it up
visual studio code or community
Whatever you're using
Sorry, but I don't believe you found the correct answer to the question by searching. That's why I asked you for a screenshot.
Mainly because I'm about 80% certain that
GetEnumerator()
is going to return IEnumerator
And I'm trying to confirm that's true before I give you the long-winded explanation of what's going on with your codewhy do you want to use getenumerator
Because that's what
foreach
is using
Please, just tell me what type GetEnumerator()
is returningStop
im getting nothing
Just tell me what type
GetEnumerator()
is returning so I can confirm why you're getting that exception
You're not getting nothing
Unless you haven't tried itfrom hovering over the var
Do you have your development environment correctly set up?
Do you get intellisense?
Alright, well, assuming you ever figure out what
GetEnumerator()
is returning, here's what's (almost certainly) happening:
foreach (V v in x)
is fancy compiler speak for this:
If e.Current
(ie, the return type of transform.GetEnumerator().Current
is an object
, it will automatically and silently be cast to the type you said in the foreach
loop, GameObject
in this case. This is an unfortunate behavior from C# 1.0 that pretty much no one runs into anymore because generic collections are much better, but I'm guessing that Unity isn't using a generic collection here, just IEnumerator
.