I playing around with one of the course games, and yes pretty new to unity.
Starting to try to split the difficulty in the level. By check on how many “waves” it has spawned
So if it is on 1-3rd wave do this
on 4th- 6th do that and so on.
But doing the followiing IF line does not work.
if (WaveCount =1) | (WaveCount <3)
But getting the error
Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type ‘int’ to ‘bool’ Assembly-CSharp C:\Users\Public\Documents\Unity Projects\Space Shooter\Assets\Scripts\GameController.cs 72 Active
What is the right way to do it?. Wavecount is an int.
The simple answer for the errors is that you were:
Using an assignment (single equal sign) rather than an equality conditional (double equal sign).
Using logical OR (single OR symbol) rather than conditional OR (double OR symbol)
Had each check in its own parenthesis rather than in one set of parenthesis.
Once the initial error had been corrected the other errors would have appeared.
if (WaveCount == 1 || WaveCount < 3)
I recommend reading through the official doc entries for ‘if’ statements and operators if you want to know more about what’s happening and how to best take advantage of them. It’s well explained with plenty of examples.
With the new solution it looks like it just add on the routine of previous wave
1-3 works fine then everything goes into a mess when hitting 4th wave.
It looks it dobbels up the number of " enemies"
IEnumerator SpawnWaves()
{
Debug.Log("Wave " + WaveCount);
yield return new WaitForSeconds(startWait);
while (true)
{
Debug.Log("Wave " + WaveCount);
if (WaveCount >= 1 && WaveCount <= 3)
{
for (int i = 0; i < hazardCount; i++)
{
// Wave 1-3 Only small asteroides.
GameObject hazard = hazards[Random.Range(0, 2)]; //hazards.Length
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
Mover mover = hazard.GetComponent<Mover>();
RandomRotator randomRotator = hazard.GetComponent<RandomRotator>();
mover.speed = Random.Range(-5, -9);
randomRotator.tumble = Random.Range(1f, 6f);
//hazardCount = 30;
//spawnWait = 0.2f;
yield return new WaitForSeconds(spawnWait);
}
}
else if (WaveCount >= 4 && WaveCount <= 6)
{
for (int i = 0; i < hazardCount; i++)
{
Debug.Log("Second challenge");
// Adding an enemy spaceship into the mix of asteroides.
GameObject hazard = hazards[Random.Range(0, 3)]; //hazards.Length
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
Mover mover = hazard.GetComponent<Mover>();
RandomRotator randomRotator = hazard.GetComponent<RandomRotator>();
mover.speed = Random.Range(-5, -9);
randomRotator.tumble = Random.Range(1f, 6f);
}
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
WaveCount++;
if (gameOver)
{
if (score > highscore)
{
highscore = score;
PlayerPrefs.SetInt("Player Score", highscore);
}
restartText.text = "Press 'R' for Restart";
//ExitText.text = "Press 'E' to exit the game";
restart = true;
break;
}
waveText.text = "Wave " + WaveCount;
}
}
Unless C# suddenly changed how “else if” behaves there is no way for the first three waves to trigger if the latter three are being executed. It’s only one or the other with “else if”. Check the for loops. The yield statement is inside the first one but outside of the second one.
I found the problem
It was one of the routines that halt the spawn 0.x seconds before spawn the next one onto the screen. It was not in the loop with the rest of the spawning routines.
just now i’m fighting “ghost” ships and asteroids i do not see. but when it hits the player or is shot , the player or the ghost exploding. Can not see what causing it.
EDIT: Found the error. A failing prefab again. Have seen it before, but a nights sleep made me see it with fresh eyes.