- Home /
Rewriting one script messed up another script
I finally got my script rewritten and working, though through testing, I've encountered some weird anomalies. First, I was only editing my Enemy Spawner script. The original way I wrote it was like this: http://pastebin.com/ce5ERhf3, and I managed to rewrite it like this: http://pastebin.com/Pe0VwdrV.
However, here are the issues that have arrised since the rewrite.
In the inspector, Enemy Drone Array Length reads 0. It should read 2. The spawner only spawns Enemy Drone 1, instead of randomly 1 or 2 (I've currently only got 2 enemies in the array). The player, controlled by my Game Manager script, no longer spawns. Nor does the level increase when the number of ships spawned hit's 30. Here is my Manager Script: http://pastebin.com/g6Sq41G3. I never touched the manager script once during my Spawner Script rewrite, and even reset the scripts to make sure they were correct in the inspector. Everything is the same as it was before, apart from the Spawner script rewrite. It's not producing any errors in the console, and I'm not really sure how to go about debugging this to figure out what is going on.
I didn't go through all that but one thing that looked a little off was the use of Random.Range on an INT which is exclusive in that the highest value will never be selected (unless it equals the lower value). Anyway, with a Random.Range of [1,2] you'll never get 2, with a Random.Range of [1,8] you'll never get 8, etc.
So I should change it to a range of 1 through .length + 1? Would that be because the range is technically 0-1 ins$$anonymous$$d of 1-2?
As I posted that I forgot for a second these are zero-filled, so 0-1 is correct; ignore what I wrote since you did use 0 as your start for Random.Range - sorry for the noise!
Well I do know why it's only spawning 1 enemy; its choosing a random number between 0 and 0, which will always be 0. Even though I initialized the length in the start function, for some reason it's not registering the proper array length.
Answer by andyisbonza · Mar 30, 2014 at 01:56 AM
Perhaps just try using random.range [0,enemyDroneArray.Length]
It might be assigning the value to the array length variable before it is properly initialised. It's better to dynamically query the array length size anyway... Gives you more flexibility in case you later decide to add to it further in the game.
Ah ha! That fixed the variable spawn issue. Now to figure out why the player isn't spawning, or is the level increasing after 30 enemies have spawned...
Where are you calling ShipCounter? I suspect your issue is that the code is never being executed. Although your ShipCounter code will work, it would be better to just divide by 30 and get the result, ignoring the remainder. You could get it down to 1 line:
return (TotalShips / 30)
This also has the advantage of not screwing things up if you call the function twice in a row. For example, if you called ShipCounter with 30 enemies killed, then you would level up. If you called it again straight away, you would still have 30 enemies killed, so you would level up again. Also, the name of the function is also a little confusing - it's not counting ships, it's checking whether you can level up.
Can you explain more fully the player not spawning issue? Is the player not appearing at all, even at the start of the game?
Also, you mentioned you don't know how to debug. Check out the Debug.Log command - use that to print out various variable values to the console.
It's controlled by my Game $$anonymous$$anager script like this:
function ShipCounter()
{
if(TotalShips % 30 == 0)
{
Level++;
}
return(Level);
}
As far as the player spawning, when I load the game I've given 2 options: Play Game and Options. Clicking Play Game causes the enemies to start spawning, and is supposed to instantiate the player as well.
I know about debug.log, I'm just not sure how to use it in a case like this, for example.
Yes, but where do you actually invoke ShipCounter? Unlike your start, update and onGui functions (which are special functions), the code in it will not execute unless you tell it to execute, so my question was "where are you telling it to execute?". You should be calling it every time an enemy is spawned.
Use Debug.Log inside of ShipCounter like this, for example:
function ShipCounter()
{
Debug.Log("Checking if the player has levelled up")
if(TotalShips % 30 == 0)
{
Level++;
}
return(Level);
}
That's gonna tell you if the code is being executed or not - I suspect it isn't.
Ah ha! I re-read my old code and found I skipped this little line somehow: level = $$anonymous$$anagerOf.ShipCounter(); So that is fixed now. Now on to why my player is no longer spawning. I've double checked the code and the inspector, everything seems to be in order.
if(Player == null && PlayerLives >= 1 && Started == true)
{
PlayerLives--;
Instantiate(PlayerShip, Vector3(0, -4.5, 2), Quaternion.identity);
Player = GameObject.FindGameObjectWithTag("Player");
}
This line of code call's the player assu$$anonymous$$g those 3 conditions are met. Inside my if(!Started) function, I have this line:
if(GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 50), "Play Game"))
{
Started = true;
Spawner.GetComponent(Enemy_Spawn_Script).IsOn = true;
}
which switches Started from false to true. Even shows correctly in the inspector that it is started (with a check mark). I really have no idea how this all happened, when all I changed was the spawner script itself, which is why I am at such a loss. Last I knew the code was working as intended.