- Home /
Question by
huzaifaehsan_2023 · Jul 08, 2020 at 12:52 PM ·
gameobject2d gameunity 2dgamecoroutine
Hi I am making a 2D game and I am having problem with this script:
using UnityEngine; using UnityEngine.SceneManagement;
public class Projectile: MonoBehaviour {
public float speed;
private Transform player;
private Vector2 target;
[SerializeField] private GameObject _explosionPrefab;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
if (transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
DestroyProjectile();
Instantiate(_explosionPrefab, transform.position, Quaternion.identity);
Destroy(other.gameObject);
SceneManager.LoadScene("Menu");
}
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
I am trying to add a wait between the destruction of other game objects and the loading of the next scene but it is not working. I tried the coroutine function but it isn't working. Help needed, please!
Comment
Answer by WildHunt777 · Jul 16, 2020 at 01:52 PM
Please, more specify what you exactly want to do. If you can delay one action before another, Coroutine should work just nicely.