- Home /
XP and Collision in Unity2D
I have used collision in the same game and it works great but for some reason this one isn't working. I am working on a top down and I am having some trouble destroying a ball after colliding with it and gaining xp from it. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tankxp : MonoBehaviour {
public float xp;
public GameObject xpbar;
void OnTriggerEnter(Collider xpcol){
if (xpcol.gameObject.tag == "xpball"){
Destroy(xpcol.gameObject);
xp += 15;
Vector3 temp = xpbar.transform.localScale;
temp.x = xp;
xpbar.transform.localScale = temp;
}
}
}
Comment
What isn’t working? Can’t you destroy your xpball or can’t you gain xp? Be sure that the xpball tag is set to the ball, and be sure that you have at least one rigidbody (on the player or on the ball) and both have a collider2d (and one is set to trigger). I also send you the possible combinations for all collision detections.
752891c8-dae5-44af-b5c9-57736324929f.png
(28.4 kB)
Is the collider set as a trigger? Add a Debug.Log("test"); line to see if the method is called. Any errors etc?