C
C#2y 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
333fred2y ago
What is the type of transform
Tvde1
Tvde12y ago
Do you have the stack trace (and line number) where the error ocurred?
Zenternion
Zenternion2y ago
line 104
Tvde1
Tvde12y ago
Which line is that in your code block
Zenternion
Zenternion2y ago
foreach(GameObject go in transform)
Angius
Angius2y ago
Well, a Transform is not a GameObject from what I see
Zenternion
Zenternion2y ago
yea
Angius
Angius2y ago
So that's the issue
Zenternion
Zenternion2y 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
333fred2y ago
Man, unity's docs are hard to read
Zenternion
Zenternion2y ago
on a phone
333fred
333fred2y 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
Zenternion2y ago
I cant find anything on the docs
333fred
333fred2y ago
Yes, that's why I'm asking you to try it in your code and tell me what it returns
Zenternion
Zenternion2y ago
k
333fred
333fred2y ago
Like, just do var x = transform.GetEnumerator();, then hover over var and tell me what it says the type is
Zenternion
Zenternion2y ago
Transform
333fred
333fred2y ago
Screenshot?
Zenternion
Zenternion2y ago
I searched it up visual studio code or community
333fred
333fred2y 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
Zenternion2y ago
why do you want to use getenumerator
333fred
333fred2y ago
Because that's what foreach is using Please, just tell me what type GetEnumerator() is returning
Zenternion
Zenternion2y ago
for (int i = 0; i < transform.childCount; i++)
for (int i = 0; i < transform.childCount; i++)
333fred
333fred2y ago
Stop
Zenternion
Zenternion2y ago
im getting nothing
333fred
333fred2y 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
Zenternion2y ago
from hovering over the var
333fred
333fred2y 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
More Posts