I have an error with my scripts PLEASE HELP ME
Hello to everyone . I have made 2d games these days. Butttt I have a lot of mistakes or error I do not know
The error is that
There are 5 diamonds in my map. Each diamond is a polygon collider. When the character touches the diamond, the diamond value increases 1 -----------> NORMAL
, but sometimes when the character touches any diamond, diamond value increases 2 I don't want that -----------> ERROR
My script is that
private void OnTriggerEnter2D(Collider2D diamondCheck) {
if (diamondCheck.gameObject.tag == "diamond")
{
Destroy(diamondCheck.gameObject);
GetComponent<AudioSource>().PlayOneShot(coin);
diamondValue= diamondValue+1;
}
Where is error I don't know please help me
This is because Your object hit with Collider two times so thats why diamondValue add two times. You can Check That only hit one time with object, you can achieve this by making Boolean variables , a simple count or change the tag of the current gameObject in the OnTriggerEnter2D Event.
Answer by developmentyussi · Apr 10, 2017 at 02:23 PM
İf I remove the Polygon collider and I add box collider the error fixed?
Answer by Cuttlas-U · Apr 10, 2017 at 05:01 PM
hi as our friend said u may get in touch with your diomend 2 times; or maybe your player has 2 collider so it works 2 times;
but u can avoid this by disabling diomond collider when u trigger with it ;
if (diamondCheck.gameObject.tag == "diamond")
{
diamondCheck.GetComponent<Collider>().enabled = false;
}