Unity freezes when adding a GameObject to a List
Since 3 days ago Unity started freezing after adding a GameObject to a List. I haven't changed anything in that particular code, so I have no idea why this is happening:
private IEnumerator HandleSelectedCubes() {
if (InputManager.instance.selectedCube != null && !selectedCubes.Contains(InputManager.instance.selectedCube)) {
if (oldSelectedCube == null) {
selectedCubes.Add(InputManager.instance.selectedCube); // This is where it crashes
oldSelectedCube = InputManager.instance.selectedCube;
} else if (InputManager.instance.selectedCube.name != oldSelectedCube.name) {
ClearSelectedCubes();
selectedCubes.Add(InputManager.instance.selectedCube);
oldSelectedCube = InputManager.instance.selectedCube;
InputManager.instance.selectedCube = null;
}
AudioManager.instance.PlayCubeSelectSound();
if (selectedCubes.Count == 4) {
InputManager.instance.allowCubeSelection = false;
moveDone = true;
yield return new WaitForSeconds(0.07f);
var destroySelectedCubes = true;
foreach (var cube in selectedCubes.ToList()) {
if (cube != null && cube.tag == "Lightning")
destroySelectedCubes = false;
}
foreach (var cube in selectedCubes.ToList()) {
if (cube != null) {
cube.GetComponent<Cube>().hasGivenShard = true;
CubeAbilities.instance.TriggerSpecialCubeAbility(cube);
if (destroySelectedCubes) {
DestroyCube(cube);
ClearSelectedCubes();
}
}
}
InputManager.instance.playerMovesCount++;
AudioManager.instance.PlayCubeDisappearSound();
ShardManager.instance.AddSessionShards(4);
}
}
}
InputManager:
private void RegisterTouchInputs() {
if (!IsScreenTouched() || !allowCubeSelection) return;
var touch = Input.GetTouch(0);
worldTouchPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
var radius = 0.1f;
var allHits = Physics2D.CircleCastAll(worldTouchPoint, radius, Vector2.zero);
// Find closest collider that was touched
var closestDist = Mathf.Infinity;
GameObject closestObject = null;
foreach (var hit in allHits) {
// Record the object if it's the first one we check, or is closer to the touch point than the previous
if ((closestObject == null || Vector2.Distance(closestObject.transform.position, worldTouchPoint) < closestDist) && hit.collider.transform.parent.tag != "Indestructible") {
closestObject = hit.collider.gameObject;
closestDist = Vector2.Distance(closestObject.transform.position, worldTouchPoint);
} else {
return;
}
//If there isn't an old cube or if the touch phase has just started, select the cube
//Or if the touch phase has started and is moving (the user is holding and selecting cubes) and previously selected cubes are the same as this one, select the cube
if ((CubeBehavior.instance.oldSelectedCube == null || touch.phase == TouchPhase.Began ||
(touch.phase == TouchPhase.Moved && CubeBehavior.instance.oldSelectedCube != null && CubeBehavior.instance.oldSelectedCube.name == closestObject.name))) {
CubeBehavior.instance.selectedCube = closestObject;
} //If the touch phase has started and is moving (the user is holding and selecting cubes) but previously selected cubes are the same as this one, ignore the cube
// This is so that it doesn't break the chain if the user goes over a differently colored cube
else if (touch.phase == TouchPhase.Moved && CubeBehavior.instance.oldSelectedCube != null && CubeBehavior.instance.oldSelectedCube.name != closestObject.name) {
return;
}
}
}
Is Input$$anonymous$$anager.instance.selectedCube
a property or a field? If it's a property, does it have any sideeffects or is it just a wrapper for a backing field?
Also i don't get your logic here. At the moment it seems impossible to select more than two cubes (at least when "ClearSelectedCubes" actually clears the "selectedCubes" List).
Furthermore your two foreach loops also seems a bit strange. Why do you call "ClearSelectedCubes" for every object in the list that isn't null and isn't tagged "Lightning"? $$anonymous$$eep in $$anonymous$$d that if all objects in your list are tagged "lightning" or somehow null (maybe because they have been destroyed) you won't clear the list at all.
It's hard to deter$$anonymous$$e the use of all this.
Just to be sure: By Unity "Hangs" you really mean it freezes like it's caught in an infinite loop?
The provided information is not enough to figure out what went wrong here.
It's a field. I edited my post and put the Input$$anonymous$$anager code as well.
If a selected object has the same name as the old one, it gets added to the list. If it doesn't, the list gets cleared and it starts from the beginning.
In some cases the objects and lists don't need to be destroyed/cleared
It freezes, as in impossible to interact with.
Answer by SohailBukhari · Sep 22, 2016 at 12:19 PM
@username What is selectedCube in your code ?? Are you sure inputmanager instance founded when you add selectedCube in list ??
Your answer
Follow this Question
Related Questions
Why does this code freeze unity? 0 Answers
Unity freezes. I dont know why... 3 Answers
loop causes freeze 0 Answers
How many instatiates per frame is too much? 0 Answers
Get axe/arrow to stick to object 0 Answers