Foreach Loop issue build vs Editor

Hello,

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?

Thanks,

John L

Editor:

\

Build:

public class HotSpotTipsManager : MonoBehaviour
{

    public ItemDataEditor itemDataBase;
    public int itemTenmoplateCount;
    public HotSpotTipTemplate[] hotspots;
    [SerializeField] int databaseIdemIndex;

    [Header("Spawn Tips")]
    public int currentTipIndex;
    public GameObject noteItemSpwanPrefab;
    public Transform noteParent;
    [SerializeField] bool hasItemTipsSpawned = false;


    // Start is called before the first frame update
    void Start()
    {
        databaseIdemIndex = StaticDataManager.currentSelectedModel; // Get the current selected model from the static data manager that has scriptable object data
        hotspots = itemDataBase.itemTemplate[databaseIdemIndex].infoHotspots.hotSpotsTemplate; // Add the hotspots from the item template to the hotspots array
    }

    /// <summary>
    /// Loop through the hotspots and spawn the tips
    /// </summary>
    public void SoawnItemTips()
    {
        if (!hasItemTipsSpawned)
        {

            foreach (var item in hotspots)
            {

                //Spawn the tips add to tipParent
                GameObject go = Instantiate(noteItemSpwanPrefab, noteParent);
                go.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = (currentTipIndex + 1).ToString();
                go.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = hotspots[currentTipIndex].hotSpotName;
                go.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = hotspots[currentTipIndex].details;
                currentTipIndex++; // Increment the current tip index

            }
            hasItemTipsSpawned = true;
        }
  
    }

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:

http://plbm.com/?p=248

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