- Home /
Weird compilation error regarding Instantiate
So this is what I get: CompilationErrorsException: script(1,12): BCE0023: Boo.Lang.Compiler.CompilerError: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
The manual says that it should be a GameObject followed by Vector3 and Quaternion, but it doesn't work for some reason. I've never had that error before.
Here's the line:
eval("Instantiate(enemy"+randomEnemyType.ToString()+",enemypos"+(randomEnemyPos+1).ToString()+".position,Quaternion.identity);");
Here's how the function looks like with the variables:
Instantiate(enemy1,enemypos1.position,Quaternion.identity);
There are 4 enemy types with a prefab for each one (GameObject) and 16 possible enemy spawn points (Transform). Both are random so it could be enemy3 and enemypos13.position, etc...
So can someone explain why this doesn't work or better yet - give me a solution to the problem?
For example, there are 4 variables for enemy types - enemy1, enemy2, enemy3, enemy4. The script generates a random number from 1 to 4 which deter$$anonymous$$es which enemy type I'm going to use. I couldn't think of another way. Even if I use ifs, there are still the positions (16 of them). That equals 64 possible position/enemy combinations.
The correct way to do that is to make an array with [enemy1, enemy2, enemy3, enemy4] and choose a random element from the array. Don't pick a random number, append it to a string, and then try to evaluate that as an object...
http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays
Your answer
Follow this Question
Related Questions
NaN exception when spawning soldiers 1 Answer
How to make random alien fire and not all of them at once.. 2 Answers
How to instanciate transform to element of a list?,How Get Transform of a instanciated Prefap 3 Answers
problems parenting a gameObject to another upon instantiating when same name exists 1 Answer
Set parent of instantiated object. 0 Answers