BCE0005: unknown identifier: 'setInterval' + prefab wont instantiate
I don't understand why setInterval() is an unknown identifier. I am trying to spawn a prefab every second, but I only get that error. here is my script:
#pragma strict
public var spawning : GameObject;
public var seconds = 1;
function Start (){
setInterval(spawn(), seconds*1000);
}
function spawn(){
Instantiate(spawning);
}
I also tried to use yield waitforseconds, but then I found my second problem. The prefab didn't spawn even though I selected it as 'spawning'.
If I already had the prefab somewhere in the scene. It spawned. but then what spawned was clones of the clones of the clones etc. after some time running, the game froze/crashed.
Answer by Fiercest · Oct 06, 2016 at 09:48 PM
Firstly, your function spawn(), is not being referenced in start, at least with what i am used to. Also your Instantiate needs a few more things, and the variables are public statics which are used for referencing in other scripts, so i changed them to normal variables. Here is the script that should work, reply if it isn't, and the error it gives, and ill try and fix it.
var spawning : GameObject; // Your object to spawn
var exampleposition : Transform; // The location where the GameObject will be spawned, assign in inspector
function Start ()
{
spawn(); // Referencing the function Spawn()
}
function spawn()
{
while (true) // This will happen forever, may assign a Boolean to it if you want to switch it on and off
{
yield WaitForSeconds (1) // Wait every 1 second
Instantiate (spawning, exampleposition.position); // Spawn your object at the exampleposition's position
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612033839im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
yield WaitForseconds not working? 1 Answer
Okay so I've built my game to an Alpha state and when I try to build it I get this. 1 Answer
NullReferenceException: Object reference not set to an instance of an object 3 Answers
If two Vector2 are same 1 Answer
Is there a way to smoothly change a float number between two numbers when a key is pressed? 1 Answer