C
C#3y ago
Zenternion

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);
}
}
public void DestroyCities() //102
{ //103
foreach(GameObject go in transform) //104
{ //105
do {
StartCoroutine(DestroyFastly(go));
} while(go != null);
}
}
Why is this happening
29 Replies
333fred
333fred3y ago
What is the type of transform
Tvde1
Tvde13y ago
Do you have the stack trace (and line number) where the error ocurred?
Zenternion
ZenternionOP3y ago
line 104
Tvde1
Tvde13y ago
Which line is that in your code block
Zenternion
ZenternionOP3y ago
foreach(GameObject go in transform)
Angius
Angius3y ago
Well, a Transform is not a GameObject from what I see
Zenternion
ZenternionOP3y ago
yea
Angius
Angius3y ago
So that's the issue
Zenternion
ZenternionOP3y ago
public void DestroyCities()
{
for (int i = 0; i < transform.childCount; i++)
{
GameObject go = transform.GetChild(i).gameObject;
public void DestroyCities()
{
for (int i = 0; i < transform.childCount; i++)
{
GameObject go = transform.GetChild(i).gameObject;
found this
333fred
333fred3y ago
Man, unity's docs are hard to read
Zenternion
ZenternionOP3y ago
on a phone
333fred
333fred3y ago
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 out
Zenternion
ZenternionOP3y ago
I cant find anything on the docs
333fred
333fred3y ago
Yes, that's why I'm asking you to try it in your code and tell me what it returns
Zenternion
ZenternionOP3y ago
k
333fred
333fred3y ago
Like, just do var x = transform.GetEnumerator();, then hover over var and tell me what it says the type is
Zenternion
ZenternionOP3y ago
Transform
333fred
333fred3y ago
Screenshot?
Zenternion
ZenternionOP3y ago
I searched it up visual studio code or community
333fred
333fred3y ago
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 code
Zenternion
ZenternionOP3y ago
why do you want to use getenumerator
333fred
333fred3y ago
Because that's what foreach is using Please, just tell me what type GetEnumerator() is returning
Zenternion
ZenternionOP3y ago
for (int i = 0; i < transform.childCount; i++)
for (int i = 0; i < transform.childCount; i++)
333fred
333fred3y ago
Stop
Zenternion
ZenternionOP3y ago
im getting nothing
333fred
333fred3y ago
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 it
Zenternion
ZenternionOP3y ago
from hovering over the var
333fred
333fred3y ago
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:
{
E e = x.GetEnumerator();
try
{
while (e.MoveNext())
{
V v = (V)e.Current;
// Body of your foreach
}
}
finally
{
... // Dispose e
}
}
{
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.
Want results from more Discord servers?
Add your server