[英]InvalidCastException: Specified cast is not valid. (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) in Unity
I am trying to Instantiate a new Enemy from the enemy class and assign it to a variable of type Enemy.我正在尝试从敌人 class 实例化一个新的敌人,并将其分配给 Enemy 类型的变量。 But I keep getting this error -> InvalidCastException: Specified cast is not valid.但我不断收到此错误-> InvalidCastException: Specified cast is not valid。 (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) . (包装器castclass) System.Object.__castclass_with_cache(object,intptr,intptr) 。
It points to this line -->它指向这一行 -->
Enemy enemyTemp = Instantiate(enemyPre, new Vector3(-0.689f, 0f, 0f), Quaternion.identity);敌人enemyTemp = Instantiate(enemyPre, new Vector3(-0.689f, 0f, 0f), Quaternion.identity); as the problem.作为问题。
Most of it can be completely ignored, but I am declaring the var with this --> public Enemy enemyPre;其中大部分可以完全忽略,但我用这个 --> public EnemyenemyPre; 声明 var;
I put in a prefab of the Enemy inside this var inside the Unity editor itself.我在 Unity 编辑器本身的这个 var 中放入了 Enemy 的预制件。 I'm using this method so I can reference the specific enemy later.我正在使用这种方法,以便以后可以参考特定的敌人。 I've tried many different solutions, but nothing seems to work.我尝试了许多不同的解决方案,但似乎没有任何效果。 I find it odd because I did almost the exact same thing with the CameraFollow class and that works just fine.我觉得这很奇怪,因为我对 CameraFollow class 做了几乎完全相同的事情,而且效果很好。 Does anyone have a solution?有没有人有办法解决吗?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
public static LevelManager instance;
public GameObject playerPre;
public CameraFollow cameraPre;
public ParticleSystem explodePlayerPre;
public ParticleSystem explodeEnemyPre;
public GameObject respawnPre;
public Enemy enemyPre;
public KillPoint killPre;
public GameObject basePre;
[SerializeField]
private Vector3 playerStartPos, respawnStartPos, cameraStartPos, killPointStartPos;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
if(PlayerPrefs.GetInt("level", 1) == 1)
{
PlayerPrefs.SetInt("level", 1);
}
}
private void OnEnable()
{
SceneManager.sceneLoaded += nextLevel;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= nextLevel;
}
public void Play()
{
SceneManager.LoadScene(PlayerPrefs.GetInt("level", 1));
}
public void LevelSelect()
{
}
private void nextLevel(Scene scene, LoadSceneMode mode)
{
if (scene.name == "MainMenu")
{
}
if (scene.name == "Level1")
{
playerStartPos = new Vector3(-1.353f, 0.45f, 0f);
Instantiate(playerPre, playerStartPos, Quaternion.identity);
Instantiate(basePre, playerStartPos, Quaternion.identity);
Instantiate(respawnPre, respawnStartPos, Quaternion.identity);
cameraStartPos = new Vector3(0f, 0f, -1f);
CameraFollow camClone = Instantiate(cameraPre, cameraStartPos, Quaternion.identity);
camClone.minX = -2.24f;
camClone.maxX = 18.4f;
camClone.minY = -2.94f;
camClone.maxY = 1.18f;
Instantiate(explodePlayerPre, playerStartPos, Quaternion.identity);
Instantiate(explodeEnemyPre, playerStartPos, Quaternion.identity);
}
if (scene.name == "Level2")
{
playerStartPos = new Vector3(-3.271f, 1.372f, 0f);
Instantiate(playerPre, playerStartPos, Quaternion.identity);
Instantiate(basePre, playerStartPos, Quaternion.identity);
Instantiate(respawnPre, respawnStartPos, Quaternion.identity);
cameraStartPos = new Vector3(0f, 0f, -1f);
CameraFollow camClone = Instantiate(cameraPre, cameraStartPos, Quaternion.identity);
camClone.minX = -2.24f;
camClone.maxX = 18.4f;
camClone.minY = -2.94f;
camClone.maxY = 3.26f;
Instantiate(explodePlayerPre, playerStartPos, Quaternion.identity);
Instantiate(explodeEnemyPre, playerStartPos, Quaternion.identity);
}
if (scene.name == "Level3")
{
playerStartPos = new Vector3(-3.492f, -0.632f, 0f);
Instantiate(playerPre, playerStartPos, Quaternion.identity);
Instantiate(basePre, playerStartPos, Quaternion.identity);
Instantiate(respawnPre, respawnStartPos, Quaternion.identity);
cameraStartPos = new Vector3(0f, 0f, -1f);
CameraFollow camClone = Instantiate(cameraPre, cameraStartPos, Quaternion.identity);
camClone.minX = -2.24f;
camClone.maxX = 24.17f;
camClone.minY = -2.94f;
camClone.maxY = 4.54f;
Instantiate(explodePlayerPre, playerStartPos, Quaternion.identity);
Instantiate(explodeEnemyPre, playerStartPos, Quaternion.identity);
Enemy enemyTemp = Instantiate(enemyPre, new Vector3(-0.689f, 0f, 0f), Quaternion.identity);
KillPoint killPointTemp = Instantiate(killPre, new Vector3(-0.689f, 0f, 0f), Quaternion.identity);
killPointTemp.Enemy = enemyTemp;
enemyTemp = Instantiate(enemyPre, new Vector3(-1.77f, 2.619f, 0f), Quaternion.identity);
killPointTemp = Instantiate(killPre, new Vector3(-1.77f, 2.619f, 0f), Quaternion.identity);
killPointTemp.Enemy = enemyTemp;
}
}
}
I solved this problem by making sure the var was storing the Class and not the Game object itself inside the Unity editor.我通过确保 var 在 Unity 编辑器中存储 Class 而不是 Game object 本身来解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.