J'ai un problème ,j'ai un script de lancement de grenade 2D et quand je la lance et quelle touche un ennemi,et ne se détruit pas alors que jai bien mit destroy (la greande est correctement instanciée en prefab)et quand je le met hors d'une coroutine il se détruit voici le code:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class explosion: MonoBehaviour
{
public Animator grenade;//ici c'est l'animator que j'ai assigné dans l'inspector des prefab
void Update()
{
if (Input.GetMouseButtonUp(0) && GameObject.Find("core").GetComponent<cor>().canBeginGame)
{
transform.position = Vector3.MoveTowards(transform.position, Input.mousePosition, 10 * Time.deltaTime);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "ENEMIE_0")//Ca c'est quand sa touche un ennemi
{
grenade.SetBool("exploser", true);
Debug.Log("EXPLOSION!",this);
transform.localScale = Vector3.one * 5;
Destroy(this.gameObject);
StartCoroutine(petage());//quand je met mon script dans une coroutine le prefab instancié il se détruit pas alors que si je le met hors dune coroutine il se detruit immédiattemnt
}
}
IEnumerator petage()
{
grenade.SetBool("exploser", true);
Debug.Log("EXPLOSION!", this);
transform.localScale = Vector3.one * 5;
yield return new WaitForSeconds(1.95f);
Destroy(this.gameObject);
}
}
MERCI D'AVNCE!