简体   繁体   English

InvalidCastException: 指定的强制转换无效 || 统一

[英]InvalidCastException: Specified cast is not valid || Unity

I got an error in Unity saying:我在 Unity 中遇到错误:
"InvalidCastException: Specified cast is not valid. “InvalidCastException:指定的演员表无效。
Tile.OnMouseDown () (at Assets/Tiles/Tile.cs:38) Tile.OnMouseDown () (在 Assets/Tiles/Tile.cs:38)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)" UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)"
To be more precise, it complains about this line(38):更准确地说,它抱怨这一行(38):
> if (OccupiedUnit.Faction == Faction.Hero) UnitManager.Instance.SetSelectedHero((BaseHero)OccupiedUnit); > if (OccupiedUnit.Faction == Faction.Hero) UnitManager.Instance.SetSelectedHero((BaseHero)OccupiedUnit);
I recently started learning Unity so I have no Idea how to fix that.我最近开始学习 Unity,所以我不知道如何解决这个问题。
Attached the code below:附上以下代码:

    if (OccupiedUnit != null)
    {
        if (OccupiedUnit.Faction == Faction.Hero) UnitManager.Instance.SetSelectedHero((BaseHero)OccupiedUnit);
        else
        {
            if(UnitManager.Instance.SelectedHero != null)
            {
                var enemy = (BaseEnemy)OccupiedUnit;
                Destroy(enemy.gameObject);
                UnitManager.Instance.SetSelectedHero(null);
            }
        }
    }
    else
    {
       if (UnitManager.Instance.SelectedHero != null)
        {
            SetUnit(UnitManager.Instance.SelectedHero);
            UnitManager.Instance.SetSelectedHero(null);
        }
    }

}

Thank you in advance.先感谢您。

Accidently I found a way to fix that.偶然我找到了解决这个问题的方法。 For some reason explicit type cast in the form of SetSelectedHero((BaseHero)OccupiedUnit) didn't work but when I changed it to SetSelectedHero(OccupiedUnit as BaseHero) it started to work properly.由于某种原因,以SetSelectedHero((BaseHero)OccupiedUnit)形式进行的显式类型转换不起作用,但是当我将其更改为SetSelectedHero(OccupiedUnit as BaseHero)时,它开始正常工作。
Don't know why it works this way but if someone has an idea I would be grateful if you could share your knowledge.不知道为什么它会这样工作,但如果有人有想法,如果你能分享你的知识,我将不胜感激。

GemuSenYou When you use SetSelectedHero((BaseHero)OccupiedUnit) and it fails, it will throw InvalidCastException, but with SetSelectedHero(OccupiedUnit as BaseHero), if it fails the result will be null, which you probably check somewhere in the code. GemuSenYou 当你使用 SetSelectedHero((BaseHero)OccupiedUnit) 并且它失败时,它会抛出 InvalidCastException,但是使用 SetSelectedHero(OccupiedUnit as BaseHero),如果它失败了,结果将是 null,你可能在代码中的某个地方检查。 That's my best quess:)这是我最好的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidCastException:指定的强制转换在 Unity 中无效 - InvalidCastException: Specified cast is not valid in Unity Unity Mirror Networking InvalidCastException:指定的演员表无效 - Unity Mirror Networking InvalidCastException: Specified cast is not valid InvalidCastException:指定的强制转换无效 - InvalidCastException: Specified cast is not valid 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid InvalidCastException:指定的强制转换在LINQ查询中无效 - InvalidCastException: Specified cast is not valid in LINQ query “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' InvalidCastException:指定的强制转换无效。 (包装器castclass)Unity中的System.Object.__castclass_with_cache(object,intptr,intptr) - InvalidCastException: Specified cast is not valid. (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM