Reputation: 737
I'm new on using Unity, and I'm trying to use a sprite as a collision trigger. But my OnTriggerEnter2d won't trigger. Here are the infos:
collision sprite used as trigger info
And code for the newPlatformRow script attached to the main character
using UnityEngine;
using System.Collections;
public class NewPlatformRow : MonoBehaviour {
private float leftPlatformX = -12f; //de unde incepe platforma din stanga
private float rightPlatformX = 3.123f; // de unde incepe platforma din dreapta
private float rowDistance = -5f; //distanta dintre randurile de platforme
private float leftPlatformWidth; //acestea vor fi calculate random pt fiecare rand nou
private float rightPlatformWidth;
// Use this for initialization
void Start ()
{
//Debug.Log("why wont you work ;_;");
}
// Update is called once per frame
void Update () {
//Debug.Log("why wont you work ;_;");
}
void OnTriggerEnter2d(Collider2D other)
{
Debug.Log("why wont you work ;_;");
if (other.gameObject.CompareTag("newPlatformRow"))
{
Vector3 newLeftPlaformPosition;
Vector3 newRightPlaformPosition;
var leftPlatform = GameObject.Find("LeftWallPlatform");
var rightPlatform = GameObject.Find("RightWallPlatform");
newLeftPlaformPosition = new Vector3(leftPlatform.transform.position.x, leftPlatform.transform.position.y + rowDistance, leftPlatform.transform.position.z);
newRightPlaformPosition = new Vector3(rightPlatform.transform.position.x, rightPlatform.transform.position.y + rowDistance, rightPlatform.transform.position.z);
Transform leftPlatformTransform = leftPlatform.transform;
Transform rightPlatformTransform = rightPlatform.transform;
Transform newLeftPlatform = Instantiate(leftPlatformTransform, newLeftPlaformPosition, leftPlatformTransform.rotation) as Transform;
Transform newRightPlatform = Instantiate(rightPlatformTransform, newLeftPlaformPosition, rightPlatformTransform.rotation) as Transform;
newLeftPlatform.parent = leftPlatformTransform.parent;
newRightPlatform.parent = rightPlatformTransform.parent;
}
}
}
Not that the: Debug.Log("why wont you work ;_;");
it's never called
I really can't figure out what am I doing wrong. Thanks
Upvotes: 1
Views: 804
Reputation: 1
You are not checking isTrigger checkbox in material component.
Upvotes: 0
Reputation: 3082
It seems that you have misspelled void OnTriggerEnter2d(Collider2D other)
.
It should actually be OnTriggerEnter2D
. Note that capital D here. All the names for triggers and colliders like OnMouseDown()
work the same way - they must have the exact same name, case sensitive.
Upvotes: 4