Hey all,
Basically, I have a box that the player has to be able to collect. When the player “collects” it, the box will actually just instantly move to another spawnpoint, and I’ll also execute scoring stuff. When the player object passes over a box, OnTriggerEnter(Collider hitObject) is called, and I do a check to make sure that the object I hit is actually a box that can be collected. If it is, I move the box to a different location to give the effect
The problem is that when my player object hits the box, OnTriggerEnter is called two or three times, which will undoubtedly cause issues for keeping track of the score. It’s my understanding that OnTriggerEnter is called once upon the initial collision with the object, so I’m not sure why it’s being called multiple times per collision.
Some of my code follows:
//From the player object's script
void OnTriggerEnter (Collider hitObject)
{
if (hitObject.tag == "peep")
{
hitObject.transform.position = new Vector3(0,-100,0);
Debug.Log("Collected");
hitObject.transform.GetComponent<peepControl>().collect();
}
}
//From the peepControl script
public void collect()
{
int i = Random.Range(0,allSpawns.Length);
while (allSpawns *== currentSpawn)*
-
i = Random.Range(0,allSpawns.Length);*
_ transform.position = allSpawns*.transform.position;_
_ currentSpawn = allSpawns;
}*
Through Debug.Log I found that the OnTriggerEnter function was testing true on its if statement three times before the object actually moved at all. Suggestions?_
Do you have multiple colliders on your player object? If so, OnTriggerEnter might get called for each collider separately?
– Licentious