- Home /
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, checkpoint
s 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.
either CheckPointTriggered is not called at all, or currentCheckpoint is larger than or equal to tottalCheckPoints.
you should learn how to use breakpoints, saves you a lot of hazzle.
Sir why it is working in Unity Editor, there is no problem with logic or checkpoint. Everything is working fine in Unity Play $$anonymous$$ode. Problem only occurs in android build.
this is just a possible guess. I had a similar issue awhile ago exclusive to android builds. if materials are generated through code, shaders do not carry over if they are not somewhere in the assets folder when you build. this casued all kinds of weird errors for me. make sure all the shaders you are using on the objects you are setting to active. are on a material in the inspector.
this is a possible guess. dont know if it applies to you.
Answer by Dmytrenko · Jul 28, 2018 at 01:44 PM
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.
right. Using "tottalCheckPoints" is not advisable. Since there are conditions in the loop inside Start not every child necessarily has to become a checkpoint. Therefore "tottalCheckPoints" might be greater than the actual Count of objects in your list. Also we don't know where and "CheckPointTriggered" is called.
Another potential issue may be the initialization of the "checkPoints" list. Currently you don't create the list manually. So we assume that this script is already attached to an object in the scene and saved / serialized in the scene as empty list. However when you create / attach this script dynamically the list won't magically initialize itself. This magic only happens inside the editor.
Thank you again Bunny83. Your second para saved me
Answer by Scramasaxe · Jul 27, 2018 at 07:55 PM
Check to see if currentCheckPoint is greater or equal to the checkPoints.Count.
Sir I have already made sure many many times that everything is correct. It also working 100% fine in Unity Editor. Problem occurs when I make a android build.
Answer by ibilalmirza · Jul 29, 2018 at 05:38 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612171652im_/https://answers.unity.com/themes/thub/images/avi.jpg)