- Home /
Logical Error In Spawn Counter (Javascript)
var home: GameObject;
var activeSpawn: int;
var countDown: float;
function Start(){
switch(home.name)
{
case "SP1": activeSpawn=0;
case "SP2": activeSpawn=1;
case "SP3": activeSpawn=2;
case "SP4": activeSpawn=3;
case "SP5": activeSpawn=4;
case "SP6": activeSpawn=5;
}
countDown=30*(activeSpawn+1);
}
The above is a sample of the relevant code in my script.
I'm trying to make a spawn counter for this game I'm making but for some reason every spawn location is treated as SP6 If you hadn't noticed my Spawn Points are SP 1-6, and home is the game object in which the script is contained each time (that's also something I was hoping for some enlightenment on, is there any better way to let the script know of the game object it's contained in?
countDown is the timer for each spawn, and activeSpawn is supposed to be the number of "active" spawn points - the logic I used was that for SP1 to be active, I'd need to have 0 active spawn points and wait 30 seconds, for SP2 I'd need to have 1 active spawn point and wait another 30 seconds (meaning 30 seconds for SP1 and 30 more seconds for SP2) and so on for the rest.
That's all the detail that came to mind if you need anything else to help me let me know
Answer by bompi88 · Dec 02, 2013 at 01:28 PM
You are missing some breaks inside your switch statements. To let the compiler know when to jump out of the case you have to put break;
after it like so:
case "SP1": activeSpawn=0; break; // treats this as one case
case "SP2": activeSpawn=1;
case "SP3": activeSpawn=2;
case "SP4": activeSpawn=3; break; // jumps out here, but treats SP2, SP3 and SP4 as one case.
So in your script you want to treat each case differently, which results in a break;
after each case.
var home: GameObject;
var activeSpawn: int;
var countDown: float;
function Start(){
switch(home.name)
{
case "SP1": activeSpawn=0; break;
case "SP2": activeSpawn=1; break;
case "SP3": activeSpawn=2; break;
case "SP4": activeSpawn=3; break;
case "SP5": activeSpawn=4; break;
case "SP6": activeSpawn=5; break;
}
countDown=30*(activeSpawn+1);
}
But, you could also do something different, based on your name pattern of your object. If you know that all spawn points you are going to use have "SP" as a prefix and the countDown variable is dependent on the number in your objects name, you could replace your switch with something like:
var activeSpawn : int = System.Int32.Parse(home.name.Substring(2));
countDown = 30 * activeSpawn;
Thanks! I can't believe the answer was so obvious, I had tried break statements by putting them along with the activeSpawn initialization inside a code block and was getting an error for it, I guess my mistake in that case was the code block not the actual break statements... I still need to get used to Javascript, it's similar enough to Java, but there's still some things about it that I don't quite get right =/ Anyway, thanks! :)
I think the second option I posted will work too, but in only one line.
Answer by anderas · Dec 02, 2013 at 01:53 PM
Just to make sure: you have the right "home" object set in every script in the inspector, right?
Regarding your question about alternatives: In C#, you can access the GameObject that your script is attached to with the member variable "gameObject". I'm sure you can do it in JS, too :)
Your answer
Follow this Question
Related Questions
Dynamically Spawning Trees Based On The Viability of the Landscape? 0 Answers
GUI Help, timer and counter 1 Answer
How to give a bonus every 1000 points? 2 Answers
Dual Timer Problems 1 Answer