- Home /
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.
You're using OnTriggerEnter
and OnTriggerExit
, and yet talk about colliding. Are you sure you know what those are supposed to do?
I know, I worked with them before. They detect collisions. Why?
Actually, they detect when a collider enters the area of another collider marked with isTrigger
. It doesn't detect a collision, as no collision occurs.
In this
public List<GameObject> GetArray()
{
return collidedWith;
before the return put the Count to debug.log. Do you have to size an Array before you can start putting things into it? allStones = GameObject.FindGameObjectsWithTag("Stone");
getyour411 : Now that's weird, too. It says 0 again. Stones are generated randomly so I can't size the array before I assign all stones into it.
Benproductions1 : Sorry, you must be mistaken. It is OnCollisionEnter that only detects and gives detailed information on enter. OnTriggerEnter is used to simply detect collisions along with the ability to detect collisions that are already colliding with the object from the beginning. So no problem here.
Answer by aeroson · Jan 25, 2014 at 10:25 AM
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");
}
I was about to tell you to copy and paste it as an answer. That worked great. Thanks a lot :)