C#
C#

help

Root Question Message

Zenternion
Zenternion10/18/2022
InvalidCastException Specified cast is not valid.

public void DestroyCities() //102
    { //103
        foreach(GameObject go in transform) //104
        { //105
            do {
                StartCoroutine(DestroyFastly(go));
            } while(go != null);
        }
    }
Why is this happening
Orannis
Orannis10/18/2022
What is the type of transform
Tvde1
Tvde110/18/2022
Do you have the stack trace (and line number) where the error ocurred?
Zenternion
Zenternion10/18/2022
line 104
Tvde1
Tvde110/18/2022
Which line is that in your code block
Zenternion
Zenternion10/18/2022
foreach(GameObject go in transform)
Angius
Angius10/18/2022
Well, a Transform is not a GameObject from what I see
Zenternion
Zenternion10/18/2022
yea
Angius
Angius10/18/2022
So that's the issue
Zenternion
Zenternion10/18/2022
public void DestroyCities()
    {
        for (int i = 0; i < transform.childCount; i++) 
        {
            GameObject go = transform.GetChild(i).gameObject;
found this
Orannis
Orannis10/18/2022
Man, unity's docs are hard to read
Zenternion
Zenternion10/18/2022
on a phone
Orannis
Orannis10/18/2022
It doesn't document what GetEnumerator() returns at all
Orannis
Orannis10/18/2022
Period
Orannis
Orannis10/18/2022
Can you tell me what transform.GetEnumerator() returns?
Orannis
Orannis10/18/2022
Because their online docs are actually bad enough that I can't find this out
Zenternion
Zenternion10/18/2022
I cant find anything on the docs
Orannis
Orannis10/18/2022
Yes, that's why I'm asking you to try it in your code and tell me what it returns
Zenternion
Zenternion10/18/2022
k
Orannis
Orannis10/18/2022
Like, just do var x = transform.GetEnumerator();, then hover over var and tell me what it says the type is
Zenternion
Zenternion10/18/2022
Transform
Orannis
Orannis10/18/2022
Screenshot?
Zenternion
Zenternion10/18/2022
I searched it up
Zenternion
Zenternion10/18/2022
visual studio code or community
Orannis
Orannis10/18/2022
Whatever you're using
Orannis
Orannis10/18/2022
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.
Orannis
Orannis10/18/2022
Mainly because I'm about 80% certain that GetEnumerator() is going to return IEnumerator
Orannis
Orannis10/18/2022
And I'm trying to confirm that's true before I give you the long-winded explanation of what's going on with your code
Zenternion
Zenternion10/18/2022
why do you want to use getenumerator
Orannis
Orannis10/18/2022
Because that's what foreach is using
Orannis
Orannis10/18/2022
Please, just tell me what type GetEnumerator() is returning
Zenternion
Zenternion10/18/2022
for (int i = 0; i < transform.childCount; i++) 
Orannis
Orannis10/18/2022
Stop
Zenternion
Zenternion10/18/2022
im getting nothing
Orannis
Orannis10/18/2022
Just tell me what type GetEnumerator() is returning so I can confirm why you're getting that exception
Orannis
Orannis10/18/2022
You're not getting nothing
Orannis
Orannis10/18/2022
Unless you haven't tried it
Zenternion
Zenternion10/18/2022
from hovering over the var
Orannis
Orannis10/18/2022
Do you have your development environment correctly set up?
Orannis
Orannis10/18/2022
Do you get intellisense?
Orannis
Orannis10/18/2022
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:
{
    E e = x.GetEnumerator();
    try
    {
        while (e.MoveNext())
        {
            V v = (V)e.Current;
            // Body of your foreach
        }
    }
    finally
    {
        ... // Dispose e
    }
}

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.
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy