Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Weirdest increment problem

Discussion in 'Scripting' started by CarlY, Nov 22, 2013.

  1. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    Hello All,

    Either I have encountered the weirdest bug ever or I'm braindead. Most likely the latter. Anyway, the problem in a nutshell. I cannot get a variable to increment! I'm pasting the relevant part.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class test : MonoBehaviour
    6. {
    7. int SpawnCounter = 0;
    8. int testme = 0;
    9.  
    10. void Update()
    11. {
    12.  CurrentTime += Time.deltaTime;
    13.         if (SpawnTimes[SpawnCounter] <= CurrentTime)
    14.         {
    15.             testme += 1;
    16.             SpawnEnemy(SpawnList[SpawnCounter],SpawnLane[SpawnCounter]);
    17.             SpawnCounter += 1;
    18.         }
    19.  
    20. }
    21.  
    The problem is "SpawnCounter" stays at 0 while testme is incremented correctly. No other part of the code modifies SpawnCounter. I'm wondering how could this be possible?? Any thoughts?
     
    Last edited: Nov 22, 2013
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354

    Was this pasted in? You have an increment on "SpawnCounter" but a private field called "SpawnCount" which is initialized to 0, is that just a typo?
     
  3. Arbelzapf

    Arbelzapf

    Joined:
    Sep 30, 2013
    Posts:
    58
    Umm, you declared the variable as "SpawnCount", yet use a variable called "SpawnCounter"

    Edit: Ninja'd
     
  4. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    Sorry that was a typo.
     
  5. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,822
    Not related, but another problem I see:

    Code (csharp):
    1. CurrentTime += Time.deltaTime;
    ...couldn't you just do this instead?

    Code (csharp):
    1. // Click on the Time.time to learn more about it!
    2. CurrentTime = Time.time;
     
  6. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    Yes, I should probably do that :)

    I've tried couple of stuff and found out that. it's impossible to change that variable's value. I've changed it's name, assigned other variables to it, etc. Nothing. It's stays at zero no matter what???
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Where are you displaying the value of SpawnCounter?
     
  8. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    If your testme variable is incrementing correctly, then the only I can think of that would break execution of your SpawnCounter increment would be an exception that's being thrown by the line before it. Are you sure that you aren't getting any errors logged to the console? Try removing the call to your SpawnEnemy method and maybe adding the following method to your test : MonoBehaviour

    Code (csharp):
    1.  
    2. private void OnGUI()
    3. {
    4.     GUILayout.Label(string.Format("SpawnCounter[{0}]", SpawnCounter));
    5. }
    6.  
    You should see its value on the game screen incrementing, which would narrow your issue down to that SpawnEnemy method.
     
  9. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Try this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class test : MonoBehaviour {
    6.  
    7. int SpawnCounter = 0;
    8. int testme = 0;
    9.  
    10. void Update() {
    11.         if (SpawnTimes[SpawnCounter] <= Time.time) {
    12.             testme += 1;
    13.             SpawnEnemy(SpawnList[SpawnCounter],SpawnLane[SpawnCounter]);
    14.             SpawnCounter += 1;
    15.         }
    16. }
    17.  
    you could even change:

    Code (csharp):
    1.  
    2. //FROM
    3. testme += 1;
    4. SpawnCounter += 1;
    5. //TO
    6. testme++;
    7. SpawnCounter++;
    8.  
     
    Last edited: Nov 22, 2013
  10. CarlY

    CarlY

    Joined:
    Nov 5, 2013
    Posts:
    29
    Thank you all for helping. As Roland pointed out issue was related with SpawnEnemy method. However, console didn't log any exceptions. I moved the increment code above the SpawnEnemy method and it's working fine now. I'll take a deeper look at that method when I have some time.