Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Foreach Loop issue build vs Editor

Discussion in 'Editor & General Support' started by John-Lisenby, Mar 5, 2024.

  1. John-Lisenby


    Nov 8, 2013

    I'm having an odd issue in my build vs in editor for foreach loop. Code below. It works in the editor but when I build it out and play it, it seems like it stuck on the first iteration. See images below. I'm using Unity 2022.3.20f Windows build using IL2CPP backend. I deleted and rebuilt Library files.

    I did notice that on a slower PC he does have less duplicates. I've also tried the function as an async task and still same issue. I've also made a test project with the same code but was unable to reproduce the issue.

    I'm not sure what else I can look at to find and fix this issue, Any suggestions?


    John L

    upload_2024-3-5_9-47-39.png \


    Code (CSharp):
    1. public class HotSpotTipsManager : MonoBehaviour
    2. {
    4.     public ItemDataEditor itemDataBase;
    5.     public int itemTenmoplateCount;
    6.     public HotSpotTipTemplate[] hotspots;
    7.     [SerializeField] int databaseIdemIndex;
    9.     [Header("Spawn Tips")]
    10.     public int currentTipIndex;
    11.     public GameObject noteItemSpwanPrefab;
    12.     public Transform noteParent;
    13.     [SerializeField] bool hasItemTipsSpawned = false;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         databaseIdemIndex = StaticDataManager.currentSelectedModel; // Get the current selected model from the static data manager that has scriptable object data
    20.         hotspots = itemDataBase.itemTemplate[databaseIdemIndex].infoHotspots.hotSpotsTemplate; // Add the hotspots from the item template to the hotspots array
    21.     }
    23.     /// <summary>
    24.     /// Loop through the hotspots and spawn the tips
    25.     /// </summary>
    26.     public void SoawnItemTips()
    27.     {
    28.         if (!hasItemTipsSpawned)
    29.         {
    31.             foreach (var item in hotspots)
    32.             {
    34.                 //Spawn the tips add to tipParent
    35.                 GameObject go = Instantiate(noteItemSpwanPrefab, noteParent);
    36.                 go.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = (currentTipIndex + 1).ToString();
    37.                 go.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = hotspots[currentTipIndex].hotSpotName;
    38.                 go.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = hotspots[currentTipIndex].details;
    39.                 currentTipIndex++; // Increment the current tip index
    41.             }
    42.             hasItemTipsSpawned = true;
    43.         }
    45.     }
  2. Kurt-Dekker


    Mar 16, 2013
    Usually differences in behavior between build and editor stem from either:

    1) incorrect resources loading (never use System.IO.File, for instance)

    2) timing issues

    In your case above I'd nominate #2 because you have a public method and nothing to ensure that Start() has been run before that method gets run.

    Here is some timing diagram help:

    Otherwise, you must debug to find out exactly what is happening in the build.

    By debugging you can find out exactly what your program is doing so you can fix it.

    Use the above techniques to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    ALSO, this is serious shenanigan code... you want to AVOID this sort of this.this.this.this nonsense:

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    Putting lots of code on one line DOES NOT make it any faster. That's not how compiled code works.

    The longer your lines of code are, the harder they will be for you to understand them.

    How to break down hairy lines of code:

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums