Can't add colliding game objects to a list.

public class Surroundings : MonoBehaviour {

	List <GameObject> collidedWith = new List<GameObject>();
	
	void OnTriggerEnter(Collider col)
	{
		collidedWith.Add(col.gameObject);
	}
	
	void OnTriggerExit(Collider col)
	{
		collidedWith.Remove(col.gameObject);
	}
	
	public List<GameObject> GetArray()
	{
		return collidedWith;
	}
	
}

This is my code on each “Stone” objects in the scene. This is supposed to return a list of colliding objects.

public class toBeDestroyed : MonoBehaviour
{
    GameObject[] allStones;

    void Permission(bool perm)
    {
        if (perm)
        {
            allStones = GameObject.FindGameObjectsWithTag("Stone");
            Begin();
        }
    }

    void Begin()
    {
        foreach (GameObject go in allStones)
        {
            print("Prints this.");
            foreach (GameObject go2 in go.GetComponent<Surroundings>().GetArray())
            {
                print("Doesn't print this.");
            }
        }
    }
}

And this script has to check that array of colliders. As you can see, second foreach is trying to access that list which isn’t happening. I tried to print “go.GetComponent().GetArray().Count” and it printed out 0 although those go objects sure had colliding objects.

All objects in my scene have rigid bodies with “is kinematic” enabled, collision detection using continuous and they have colliders with “is trigger” enabled. I used to return those values with no problem before. I haven’t checked my project in a few days and when I was back, bam : problem.

So what could be the problem? Thanks in advance.

When exactly are you calling the Permission function ? Are you sure its being called after first FixedUpdate ? (if you call it before first physics update the triggers have not yet collided)

“Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.”

Try to do something like this instead.

int fixedUpdatesCounter=0;
void FixedUpdate() {
	fixedUpdatesCounter++;
	if(fixedUpdatesCounter==2) {
		Debug.Log("First fixed update done");
	}
}

Or

void Start() {
	StartCoroutine("DelayedStart");
}	
IEnumerator DelayedStart() {
 	yield return new WaitForFixedUpdate();
	Debug.Log("First fixed update done");
}