- Home /
No appropriate version of 'UnityEngine.Object.Instantiate'
I'm having a problem with this script:
public var enemySpawnLimit : int; // Sets the number of Enemys you wanna spawn at this location
public var radiusmax: float; // The radius maximum.
private var radiusx=0.0; // x co-ordinate
private var radiusy=0.0; // y co-ordinate
private var radiusz=0.0; // z co-ordinate
//---------------------------------------------------------------------------------------//
function Start () {
// for (var y = 0; y < enemySpawnLimit; y++) { // If you wanna spawn the Enemys upwards, say for flying
for (var x = 0; x < enemySpawnLimit; x++) { // this code is to cycle through spawning until X amount of enemys has arrived
radiusx=Random.Range(0.0,radiusmax); // Everytime A cube is spawned a it spawns at a random x,y,z location
radiusy=Random.Range(0.0,5.0);
radiusz=Random.Range(0.0,radiusmax);
Instantiate(enemy, Vector3 (radiusx, radiusy, radiusz), Quaternion.identity); // the spawning script bit
}
}
//}
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
function Update () {
}
//---------------------------------------------------------------------------------------//
Not sure how to fix it. as I get this error.
Assets/Scripts/enemy.js(22,24): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(System.Type, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
You need to pass Instantiate
a Prefab not a Script/Type/Class
Basically, 'enemy' should be a GameObject or Prefab. I'm guessing the only reference to 'enemy' your script can find is enemy.js which is itself a script.
Yeah but when I name my model 'enemy' it doesn't work
You can't have two things with the same name. Take this for example:
var enemy:enemy;
function Start() {
Instantiate(enemy);
}
How is Unity supposed to find out which is which?
You either rename your script, or rename your variable. (You should give ALL your scripts capitols. It's just how things should be)
Answer by rutter · Oct 20, 2013 at 05:50 AM
This line:
Instantiate(enemy, Vector3 (radiusx, radiusy, radiusz), Quaternion.identity);
Judging by the compiler error, enemy
is the name of a script you wrote, not a variable in the code you posted.
Instantiate() is usually used to spawn a prefab instance. Perhaps you could create an inspector variable, then use that? There are plenty of tutorials around the net, but here's a quick example:
public var enemyPrefab : GameObject;
...
//in some function
Instantiate(enemyPrefab, someVector, someRotation);
It's more important to understand what the code does, even if it takes a bit longer. I suggest you read the link I posted.
i've looked through it all and I'm thinking it might not work because I haven't created a object I created an $$anonymous$$odel for the sprite and tried attaching to it. Would this be the problem? my mate was the one who wrote the code and it works fine on his PC just when he sent me it; it hasn't worked properly. I'm a little new to Unity I've only just managed to get my head around the HP/XP system and my character running etc lol
A "$$anonymous$$odel" in Unity is an object. It can act as a prefab
Your answer
Follow this Question
Related Questions
Enemy not spawning correctly 1 Answer
How to reduce the chances of a particular enemy spawning ? 3 Answers
Need help with enemy spawner 2 Answers
Spawning enemy mobs .......... Instantiate woe ? 2 Answers
Trying to write enemy spawn in JS. 2 Answers