Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Run a method outside of update

Discussion in 'Scripting' started by v_chs, May 22, 2020.

  1. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    I created a method that simulates fire movement. This movement gets triggered from an external script but my problem is that I cannot start this method outside of Update. I need to call and start this method on Start because it's too expensive to run this in each frame, and the Instantiate is too fast -it should instantiate slowly. The problem is that on Start doesn't run.

    Is there any way to run this method on start?

    Here's my sample code.

    Code (CSharp):
    1.     [HideInInspector] public bool isMoving; // State variable that checks if the fire isMoving
    2.     public GameObject fire; // the var that holds teh fire object
    3.     [SerializeField] private float speed; // fire speed
    4.  
    5.  
    6.     private void Update() {
    7.         FireMoveCheck();
    8.     }
    9.  
    10.     private void FireMoveCheck() {
    11.         if (isMoving) {
    12.             Invoke(nameof(MovingFire), 2);
    13.         }
    14.         else if (isMoving == false) {
    15.             CancelInvoke();
    16.         }
    17.     }
    18.  
    19.     private void MovingFire() {
    20.         var degrees = 0;
    21.         degrees = GetCompassInput(degrees);
    22.         gameObject.transform.position = transform.TransformPoint(speed / 3600, speed / 3600, 0);
    23.         gameObject.transform.rotation = Quaternion.Euler(-90, 0, degrees + 45);
    24.         var position = gameObject.transform.position;
    25.         var rotation = gameObject.transform.rotation;
    26.         Instantiate(fire, position, rotation);
    27.     }
    28.  
    29.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'm not sure I fully unerstand the issue you are having, but have you tried InvokeRepeating?
     
    v_chs likes this.
  3. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    Try looking into Coroutines. They allow you to move at your own pace, using things like WaitForSeconds. In particular WaitUntil could be interesting for you if you want to react to input.
     
    Pagefile, v_chs, Vryken and 1 other person like this.
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Have you thought about simply enable, and disable the script containing your Update method, when you want the FireMoveving stuff going on, or be paused?
     
    v_chs likes this.
  5. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64

    Sorry for the late reply. My problem is that if I move the FireMoveCheck on Start, it doesn't run, as it runs inside Update. I'll try the solution you suggest and I'll let you know. Thanks!
     
  6. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    Sorry for the late reply. I'll check Coroutines, and I'll let you know. Thanks a lot!
     
  7. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    Sorry for the late reply. Hmm, I didn't think of this approach. Maybe I should consider it. Thanks!
     
    arfish likes this.