- Home /
Spawning script error
So i need to make a spawner that spawns at a random location given. If there's anything wrong with this code tell me and help me fix it.
#pragma strict
var spawnObject : GameObject;
var spawnPoint : GameObject;
var spawnCounter : int= 0;
var spawnCounterMax : int= 0;
function Update () {
this.spawnCounter++;
if (this.spawnCounter >= this.spawnCounterMax){
Instantiate(this.spawnObject, this.spawnPoint.transform.position.x + Random.Range(-2,2),
this.spawnPoint.transform.position.y + Random.Range(6.23,6.3),
this.spawnPoint.transform.position.z + Random.Range(-10,-10.1),
this.spawnPoint.transform.rotation);
this.spawnCounter = 0;
What is it doing or not doing that you need it to do? One issue is how you are "ti$$anonymous$$g" your spawning. Frames don't come at perfectly even rate, and the fps will vary greatly from device to device. Look up answers with 'timer' or 'timestamp'.
NO. Answers is not the place to beg for people to write scripts for you.
If you have specific questions, people are happy to give specific answers.
Otherwise, i suggest you look under collaboration and work for hire in the forums.
Well for one, you're only going to spawn anything if the spawn counter is greater than the spawnCounter$$anonymous$$ax, did you mean to say <= (lower than or equal to) as opposed to >= (greater than or equal to)?
Also, why are you setting spawn counter to 0 after each instantiation? That's going to cause you to enter an endless loop. $$anonymous$$oving the spawnCounter ++ into the if block of code would make far more sense. That would increase it after every instantiation. Once you reach your max, you can then set the spawnCounter to 0.
In my opinion, this entire section of code should be moved out of update and into a dedicated spawn function that you can call when you need it.
Since spawnCounter counts frames, I imagine you have some specific time you want to way between spawns. One way to hadle this issue is to use InvokeRepeating(). Another is a coroutine, and a third is to use a timestamp within update. If you search for these three topics, you will find many posts.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
touch input certain screen part 1 Answer
Rotation(Y) and Acceleration Camera 0 Answers