I have a prefab with a child that has a kinematic Rigidbody 2D and a Box Collider 2D that is a Trigger. The prefab gets instantiated using this script:
static var canSpawn = true;
var platform : GameObject;
var heightMax : Transform;
var heightMin : Transform;
var time : float;
function Update () {
var mousePos = Input.mousePosition;
mousePos.z = 10;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
if(Input.GetButtonDown("Fire1") && objectPos.y < heightMax.position.y - 0.5 && objectPos.y > heightMin.position.y + 0.5 && canSpawn){
var clone = Instantiate(platform, objectPos, Quaternion.identity);
Destroy(clone, time);
}
}
with platform being the prefab.
Attached to the player - which also has a kinematic Rigidbody 2D and a Box Collider 2D - is this script:
var left : Transform;
var up : float;
var back : float;
var time : float;
function OnTriggerEnter2D (other : Collider2D) {
if(other.transform == left){
Debug.Log("test");
ConstantMovement.canMove = false;
rigidbody2D.velocity.y = up;
rigidbody2D.velocity.x -= back;
}
}
function OnTriggerExit2D (other : Collider2D) {
if(other.transform == left){
yield WaitForSeconds(time);
ConstantMovement.canMove = true;
}
}
This works as expected when placing the prefab in the scene and attaching the child to the Transform left. However, when using it with the instantiated objects, it fails at “if(other.transform == left)”. I believe this is because the instantiated objects are being created as clones. So how should I go about detecting the collision with the clones?