- Home /
The question is answered, right answer was accepted
Why does this not work (Involves instantiating objects in response to a collision*)
Ok, so I am making an endless runner, and I am trying to make it so when the "player" object collides with a "marker" object it will spawn the next section of road (which contains another marker). All the road sections behind the player are deleted as they pass. My problem is that when the player collides with the marker, the next section of the road does not appear. I can't figure out what I did wrong.
Here's the Code:
using UnityEngine;
using System.Collections;
public class MarkerCollisionCheck : MonoBehaviour {
public GameObject objectToSpawn = null;
public GameObject spawnPoint = null;
public bool spawning = false;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player" && spawning == false)
{
spawning = true;
Instantiate(objectToSpawn, spawnPoint.transform.position, Quaternion.identity);
spawning = false;
}
}
}
Just in case I am misunderstanding, "Instantiate" spawns the object correct? Notice that the "spawnPoint" is another object. If you need more information, just ask.
Thank You, expat1999
code is correct.
For correct collision with trigger -
One of the 2 colliders must be marked as Trigger.
For collision to register. One of the 2 Colliders must be Rigidbody.
To check if collision is happening use Debug.Log
Thank you Prlyanshu! $$anonymous$$ade a really dumb mistake... On the bright side I now have some practice making questions on here :D
Answer by expat1999 · Aug 07, 2015 at 11:44 AM
Commented by Prlyanshu
code is correct.
For correct collision with trigger -
One of the 2 colliders must be marked as Trigger.
For collision to register. One of the 2 Colliders must be Rigidbody.
To check if collision is happening use Debug.Log