'WaitForSeconds' does not contain a constructor that takes 1 arguements

Whenever I do “yield return new WaitForSeconds(5);” It gives me an error stating ‘WaitForSeconds’ does not contain a constructor that takes 1 arguments. I can’t find a similar time this has happened before on any forum, at least in the same way, from where I looked, and it doesn’t make sense as I don’t know what exactly that means? And or why it’s appearing, because even according to the documentation for 2022.3, this is how your meant to do a WaitForSeconds. Is there something I did wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class testingThing : MonoBehaviour, IInteractable 
{
    public Animator animator;

    public void Interact()
    {
        Debug.Log(Random.Range(0, 100));
        FadeToLevel(1);
    }

    public void FadeToLevel (int levelIndex)
    {
        animator.Play("Crossfade_Start");
        StartCoroutine(YourName());
    }    

    IEnumerator YourName()
    {
        yield return new WaitForSeconds(5);
        Debug.Log("Finished waiting!");
        SceneManager.LoadScene("LevelOne");
    }   
}

1 Like

A constructor is what you use to make an instance of a C# class.

You can see the docs on the constructor for WaitForSeconds here: Unity - Scripting API: WaitForSeconds.WaitForSeconds

If you’re getting this error, I’d wager you have a script elsewhere called WaitForSeconds in your project.

2 Likes

Ah, I’m just stupid. Found it, thanks lmao.

1 Like

You most likely created your own custom class or type with the name WaitForSeconds. That’s why it’s always good to make sure you copy the exact error message. If the compiler would actually refer to Unity’s WaitForSecond class, it would have said “UnityEngine.WaitForSeconds” in the error message. If there’s no namespace, the class would be in the global namespace and therefore most likely created by you or some package you included.

Avoid naming your own types after Unity’s common type names. If you really need to, your own type should be in a separate namespace as well so it’s easier to distinguish them.

2 Likes