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

Is it a problem to call a for loop inside Update ?

Discussion in 'Scripting' started by dubiduboni_unity, May 5, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    I mean it will effect the game performance or something ?

    This script is attached to empty GameObject and control over each GameObject that have attached the Shooting script. And instead changing each time the objects with the Shooting scripts one by one this control all of them while the game is running by calling the ShootingSettings method in the Update.

    This is working fine. I'm just wondering if it's a good thing to call the method ShootingSettings from the Update and then making the for loop all the over and over again nonstop ? Or inside the Update or inside the ShootingSettings I should make some checks and only if one of the variables values has changed then calling the for loop ?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class ShootingManager : MonoBehaviour
    8. {
    9.     [Header("Main")]
    10.     public float launchForce = 700f;
    11.     public bool automaticFire = false;
    12.     public float bulletDestructionTime;
    13.  
    14.     [Space(5)]
    15.     [Header("Slow Down")]
    16.     public float maxDrag;
    17.     public float bulletSpeed;
    18.     public bool bulletsSlowDown = false;
    19.     public bool overAllSlowdown = false;
    20.     [Range(0, 1f)]
    21.     public float slowdownAll = 1f;
    22.  
    23.     private List<Shooting> shooters;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         shooters = FindObjectsOfType<Shooting>().ToList();
    29.         ShootingSettings();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         ShootingSettings();
    36.     }
    37.  
    38.     private void ShootingSettings()
    39.     {
    40.         if (shooters.Count > 0)
    41.         {
    42.             for (int i = 0; i < shooters.Count; i++)
    43.             {
    44.                 shooters[i].GetComponent<Shooting>().launchForce = launchForce;
    45.                 shooters[i].GetComponent<Shooting>().automaticFire = automaticFire;
    46.                 shooters[i].GetComponent<Shooting>().bulletDestructionTime = bulletDestructionTime;
    47.                 shooters[i].GetComponent<Shooting>().maxDrag = maxDrag;
    48.                 shooters[i].GetComponent<Shooting>().bulletSpeed = bulletSpeed;
    49.                 shooters[i].GetComponent<Shooting>().bulletsSlowDown = bulletsSlowDown;
    50.                 shooters[i].GetComponent<Shooting>().overAllSlowdown = overAllSlowdown;
    51.                 shooters[i].GetComponent<Shooting>().slowdownAll = slowdownAll;
    52.             }
    53.         }
    54.     }
    55. }
    56.  
     
  2. PrieDayThor

    PrieDayThor

    Joined:
    Jun 18, 2015
    Posts:
    16
    The loop itself is fine but using GetComponent that often is not recommended.
    You don't need to call GetComponent in this case, because
    Code (csharp):
    1. shooters[i]
    will give you an object of the type "Shooting".
    Just use
    Code (csharp):
    1. shooters.launchForce = launchForce;
     
    Ryiah and dubiduboni_unity like this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Looping is fine, but avoid infinite loops such as:
    Code (CSharp):
    1. while(true) {
    2.  
    3. }
    These will hang your application.

    If you must use an infinite loop for any reason, use a Coroutine to do so instead:
    Code (CSharp):
    1. IEnumerator InfiniteLoop() {
    2.    while(true) {
    3.       //do some stuff
    4.       yield return //timing option
    5.    }
    6. }
     
    Ryiah and dubiduboni_unity like this.
  4. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    I'm not sure if I need infinite loops. The thing is that if I will not call the method ShootingSettings in the Update it will not effect the changes in real time when the game is running.

    That's why I wanted to call it from the Update too.
     
  5. PrieDayThor

    PrieDayThor

    Joined:
    Jun 18, 2015
    Posts:
    16
    Well... we don't know what you want to achieve with your system.
    You could give use a description and we could give you some suggestions to improve it.
     
  6. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    you should never use GetComponent in a update loop unless it is required and there is no other way to effectively store the reference.

    it's far better to store a reference in the required component to the next component. and so on.