help
Root Question Message
transform
Transform
is not a GameObject
from what I seepublic void DestroyCities()
{
for (int i = 0; i < transform.childCount; i++)
{
GameObject go = transform.GetChild(i).gameObject;
found thisGetEnumerator()
returns at alltransform.GetEnumerator()
returns?var x = transform.GetEnumerator();
, then hover over var
and tell me what it says the type isGetEnumerator()
is going to return IEnumerator
foreach
is usingGetEnumerator()
is returningfor (int i = 0; i < transform.childCount; i++)
GetEnumerator()
is returning so I can confirm why you're getting that exceptionGetEnumerator()
is returning, here's what's (almost certainly) happening:foreach (V v in x)
is fancy compiler speak for this:{
E e = x.GetEnumerator();
try
{
while (e.MoveNext())
{
V v = (V)e.Current;
// Body of your foreach
}
}
finally
{
... // Dispose e
}
}
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
.