List<> not working in Android build, But working fine in Unity editor.

I am making a List of GameObjects called checkpoints. At the start, all of the GameObjects are set to inactive in the editor. When the player starts an event, checkpoints get activated one by one in a sequence. When a vehicle triggers a checkpoint, the next checkpoint gets activated. Everything works fine in Unity Play Mode without any error or warning.

However, in the Android build, the following statement checkPoints[currentCheckPoint].SetActive(true); doesn’t work and all the code after it does not get executed. I’m only posting relevant code because the full script is a bit lengthy.

public List<GameObject> checkPoints;
    
static int tottalCheckPoints = 0;
int currentCheckPoint = 0;

private void Start()
{
    tottalCheckPoints = transform.childCount;
    for (int i = 0; i < tottalCheckPoints; i++)
    {
        if (transform.GetChild(i).tag == "CheckPoint" || transform.GetChild(i).tag == "FinaleCheckPoint")
        {
            checkPoints.Add(transform.GetChild(i).gameObject);
        }
    }
}

public void CheckPointTriggered()
{
    if (currentCheckPoint < tottalCheckPoints)
    {
        checkPoints[currentCheckPoint].SetActive(true); /*<------ that statement and all the statements after that doesn't get executed in android build.*/
        currentCheckPoint++;
    }
}

I used text logging in Android by printing specific text on screen after every statement to see from where the code stops working and found that the following statement doesn’t get executed (or any other statement after that). checkPoints[currentCheckPoint].SetActive(true);

I am using latest version of Unity 2018.1.4.f1.

Works fine on my device, judging by adb logs. I have tested it with empty objects, on Unity 2018.2.1f.

There is a possibility that your issue can be somehow connected to tags you using. There was a case in my practice when in the android build the tags assigned to some objects in the editor did not appear in the build. Why did it happen, and how to solve it I don’t know.

Try test with some else object identifiers, maybe names. Also log whether your checkpoints have tags.

And as advice: don’t ignore try-catch’es and logging. For example,

if (currentCheckPoint < tottalCheckPoints)
     {
         checkPoints[currentCheckPoint].SetActive(true); /*<------ that statement and all the statements after that doesn't get executed in android build.*/
         currentCheckPoint++;
     }

tottalCheckPoints potentially can be greater than checkpoint.Count and it would be an index exception.

Check to see if currentCheckPoint is greater or equal to the checkPoints.Count.

Finlay, Issue is solved. There was some problem with Unity editor or my computer I don’t know. Earlier I had restarted Unity many times, issue was not solved. I usually don’t shutdown my computer often. So today I have restarted my computer and build again and every thing started working in the build too