Search Unity

Check if the varisble is within range

Discussion in 'Getting Started' started by PetterL68, Feb 2, 2019.

  1. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    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.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,199
    Like this.

    Code (csharp):
    1. if (WaveCount >= 1 && WaveCount <= 3)
    2. {
    3.     // waves 1-3
    4. }
    5. else if (WaveCount >= 4 && WaveCount <= 6)
    6. {
    7.     // waves 4-6
    8. }
    The simple answer for the errors is that you were:
    1. Using an assignment (single equal sign) rather than an equality conditional (double equal sign).
    2. Using logical OR (single OR symbol) rather than conditional OR (double OR symbol)
    3. 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.
    Code (csharp):
    1. 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.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/index
     
    Last edited: Feb 2, 2019
    JoeStrout likes this.
  3. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    Thanks Ryiah.

    Not to familiar with the signs for AND and OR yet.
    Have done a bit Visual Basic earlier . So I think I need to learn the difference.

    Petter

    Ryiah
     
  4. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    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"

    Code (CSharp):
    1.  
    2.  
    3. IEnumerator SpawnWaves()
    4.     {
    5.         Debug.Log("Wave  " + WaveCount);
    6.         yield return new WaitForSeconds(startWait);
    7.         while (true)
    8.         {
    9.         Debug.Log("Wave  " + WaveCount);
    10.         if (WaveCount >= 1 && WaveCount <= 3)
    11.             {
    12.                 for (int i = 0; i < hazardCount; i++)
    13.            
    14.                 {
    15.                     // Wave 1-3   Only small asteroides.
    16.                     GameObject hazard = hazards[Random.Range(0, 2)]; //hazards.Length
    17.                     Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    18.                     Quaternion spawnRotation = Quaternion.identity;
    19.                     Instantiate(hazard, spawnPosition, spawnRotation);
    20.                     Mover mover = hazard.GetComponent<Mover>();
    21.                     RandomRotator randomRotator = hazard.GetComponent<RandomRotator>();
    22.                     mover.speed = Random.Range(-5, -9);
    23.                     randomRotator.tumble = Random.Range(1f, 6f);
    24.  
    25.                     //hazardCount = 30;
    26.                     //spawnWait = 0.2f;
    27.  
    28.  
    29.                     yield return new WaitForSeconds(spawnWait);
    30.                 }
    31.  
    32.             }
    33.         else if (WaveCount >= 4 && WaveCount <= 6)
    34.             {
    35.              for (int i = 0; i < hazardCount; i++)
    36.                   {
    37.                     Debug.Log("Second challenge");
    38.         // Adding an enemy spaceship into the mix of asteroides.
    39.                     GameObject hazard = hazards[Random.Range(0, 3)]; //hazards.Length
    40.                     Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    41.                     Quaternion spawnRotation = Quaternion.identity;
    42.                     Instantiate(hazard, spawnPosition, spawnRotation);
    43.                     Mover mover = hazard.GetComponent<Mover>();
    44.                     RandomRotator randomRotator = hazard.GetComponent<RandomRotator>();
    45.                     mover.speed = Random.Range(-5, -9);
    46.                     randomRotator.tumble = Random.Range(1f, 6f);
    47.                     }
    48.                 yield return new WaitForSeconds(spawnWait);
    49.                
    50.             }
    51.  
    52.         yield return new WaitForSeconds(waveWait);
    53.  
    54.  
    55.             WaveCount++;
    56.             if (gameOver)
    57.             {
    58.                 if (score > highscore)
    59.                 {
    60.                     highscore = score;
    61.                     PlayerPrefs.SetInt("Player Score", highscore);
    62.                 }
    63.              
    64.                 restartText.text = "Press 'R' for Restart";
    65.                 //ExitText.text = "Press 'E' to exit the game";
    66.                 restart = true;
    67.                 break;
    68.             }
    69.             waveText.text = "Wave " + WaveCount;
    70.         }
    71.     }
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,199
    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.
     
    Last edited: Feb 3, 2019
  6. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    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.
     
    Last edited: Feb 4, 2019