Home > Software engineering >  Cant use IEnumerator and WaitForSeconds
Cant use IEnumerator and WaitForSeconds

Time:07-07

I am trying to write a code in Unity2D that displays a different gameobject after each set amount of time. I am a complete beginner and just started today. This is my code

using System.Collections.Generic;
using UnityEngine;

public class DestroyEnemy : MonoBehaviour
{
    public GameObject anim;
    public GameObject anim2;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Run());
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    
        void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.tag == "FriendlyBullet")
            {
                IEnumerator run()
            {
                Destroy(gameObject);
                Instantiate(anim, transform.position, transform.rotation);

                yield return new WaitForSeconds(2);

                Instantiate(anim2, transform.position, transform.rotation);
            }
                

            }

        }
    }
    

The error message says "The local function "run" is declared but never used." What is wrong here?

CodePudding user response:

First of all, I'd like to point out, that you're calling your coroutine only at the start. As a result, it won't ever run again while the game is already running. I'm assuming the game is a kind of shooter and you want to run the coroutine function when the bullet hits the enemy. So first check if that's what you really want and otherwise I'd move the StartCoroutine to OnCollisionEnter2D. Secondly, as UnholySheep pointed out, you've missed a capital letter. You're calling run but you have only declared Run. Yes, that's a difference, C# is case sensitive. Functions in C# are supposed to start with a capital letter :). That's why the IDE or whatever editor is telling you that you declared a function, but you never used it. Local function means that it's declared inside of another function and won't be accessible anywhere else.

It's hard to fix a piece of code from the top of the head as I'm not all that experienced in Unity, only a couple of months so far, but I've fixed those said issues and it should help :).

using System.Collections;
using UnityEngine;

public class DestroyEnemy : MonoBehaviour
{
    public GameObject anim;
    public GameObject anim2;

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("FriendlyBullet"))
        {
            StartCoroutine(Run());
        }
    }
    
    IEnumerator Run()
    {
        Destroy(gameObject);
        Instantiate(anim, transform.position, transform.rotation);

        yield return new WaitForSeconds(2);

        Instantiate(anim2, transform.position, transform.rotation);
    }
}

PS. I've switched your tag logic for CompareTag as it seems to be a better option for doing the same thing :).

Good luck with your game!

  • Related