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. Dismiss Notice

Timer Elapsed Event Args Method Only Executing One Line?

Discussion in 'Scripting' started by zapman502, Apr 11, 2022.

  1. zapman502

    zapman502

    Joined:
    Apr 13, 2020
    Posts:
    14
    So I setup a timer, and created a private void method that is executed when the elapsed event is raised for that timer. Problem is, it will execute only ONE line in the method, the first one, then ignore the rest. Is this normal? Is there another way to just put everything I want to happen in that one method so I don't have to use this one to set a flag true and then use that flag in a conditional statement to execute another method?

    Code (CSharp):
    1.     void iniVariables(){
    2.         _statInt_Hunger = 100;
    3.        
    4.         tmr_monTick = new Timer();
    5.         tmr_monTick.Interval = 1000;
    6.         tmr_monTick.Elapsed += new ElapsedEventHandler(Tick);
    7.         tmr_monTick.Start();
    8.     }
    9.  
    10.     private void Tick(object sender, ElapsedEventArgs e){
    11.         --_statInt_Hunger; //only line that excecutes
    12.         print("Hello");
    13.         Vector3 foodposition =  new Vector3(food.position.x, food.position.y, 0);
    14.             if (_statInt_Hunger < 50){
    15.                 agent.SetDestination(foodposition);
    16.                 print(foodposition);
    17.             }
    18.  
    19.     }
     
  2. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,194
    You could potentially just do
    Code (CSharp):
    1. InvokeRepeating(nameof(MyMethod), 100f, 100f);
     
    zapman502 likes this.
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    i would avoid the timer class and use one of unity's features for this, could use InvokeRepeating like mentioned. Or personally i would just track time in your Update function and invoke your function from there.

    Code (CSharp):
    1. private void Update() {
    2.     if (elapsed > duration) {
    3.         MyMethod();
    4.         elapsed = 0f;
    5.     }
    6.     elapsed += Time.deltaTime;
    7. }
    8.  
     
    zapman502 likes this.
  4. zapman502

    zapman502

    Joined:
    Apr 13, 2020
    Posts:
    14
    Thank you! I ended up using this format, but I will read up on the InvokeRepeating method that sstrong shared to learn it's uses. I read the Timer class has something to do with threading or at least running in some other thread so maybe that process is somehow interrupting the execution of the rest of the method? I wont be using it in the future anyway but yeah that was a curious issue. Thanks again.
     
    passerbycmc likes this.
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    yeah when it comes to doing stuff based on time in Unity, you really need to make sure things are happening on the main thread. Your tools for dealing with it, are your various Update methods and features like Coroutines.