StartCoroutine not working

Help me pls.

its selectedItem.cs

using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.Collections;

public class selectedItem : MonoBehaviour
{
string targetImg;
bool ans = false;

public static int mustDestroyed;

private void Awake()
{
    mustDestroyed = -1;
}

private void OnMouseDown()
{
    targetImg = gameObject.name.Remove(gameObject.name.Length - 7);
    ans = false;

    for (int i = 0; i < gm.names.Length; i++)
    {
        if (targetImg == gm.names*.name)*

{
gm.rightAnswer++;
mustDestroyed = i;
Destroy(gm.clonImg*);*
ans = true;
} else if (!ans && i == gm.names.Length - 1)
{
gm.wrongAnswer++;
print("wrong answer is " + gm.wrongAnswer);
ans = true;
}
}
if (gm.rightAnswer == gm.count)
{

StartCoroutine(wait());
//if (SceneManager.GetActiveScene().buildIndex + 1 < SceneManager.sceneCountInBuildSettings)
// nextLevel(“Level” + (SceneManager.GetActiveScene().buildIndex + 1));
//else
// nextLevel(“MainMenu”);
}
}
IEnumerator wait()
{
yield return new WaitForSeconds(1f);
if (SceneManager.GetActiveScene().buildIndex + 1 < SceneManager.sceneCountInBuildSettings)
nextLevel(“Level” + (SceneManager.GetActiveScene().buildIndex + 1));
else
nextLevel(“MainMenu”);
}
private void nextLevel(string level)
{
SceneManager.LoadScene(level);
}
}
//////////////////////////////////////////////
its mm.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class mm : MonoBehaviour
{
[System.Serializable]
public class Level
{
public string LevelText;
public int score;
public int UnLocked;
public bool IsInteractable;
}
public GameObject levelButton;
public Transform spacer;
public List levelList;
private Transform[] mRespPoint;
private int allStars;
private void Start()
{
GetPointsPos();
FillList();
}
private void FillList()
{
allStars = 0;
for(int i = 0; i < levelList.Count; i++)
{
GameObject newButton = Instantiate(levelButton, mRespPoint*.position, Quaternion.identity) as GameObject;*
LevelButton button = newButton.GetComponent();
button.LevelText.text = levelList*.LevelText;*
if (PlayerPrefs.GetInt(“Level” + button.LevelText.text) == 1)
{
levelList*.UnLocked = 1;*
levelList*.IsInteractable = true;*
}
levelList*.score = PlayerPrefs.GetInt(“Level” + button.LevelText.text + "score");
_if (levelList.score > 0)*

{
button.Star1.SetActive(true);
}
if (levelList*.score > 1)*
{
button.Star2.SetActive(true);
}
if (levelList*.score > 2)*
{
button.Star3.SetActive(true);
}
allStars += levelList*.score;*
button.unlocked = levelList*.UnLocked;*
button.GetComponent().interactable = levelList*.IsInteractable;*
button.GetComponent().onClick.AddListener (() => LoadLevels(“Level” + button.LevelText.text));
newButton.transform.SetParent(spacer);
}
SaveAll();
}
private void LoadLevels(string value)
{
SceneManager.LoadScene(value);
}
private void SaveAll()
{
if (PlayerPrefs.HasKey(“Level1”))
{
return;
}
else
{
GameObject[] allButtons = GameObject.FindGameObjectsWithTag(“LevelButton”);
foreach (GameObject buttons in allButtons)
{
LevelButton button = buttons.GetComponent();
PlayerPrefs.SetInt(“Level” + button.LevelText.text, button.unlocked);
}
}
}
private void GetPointsPos()
{
int num = gameObject.transform.childCount;
mRespPoint = new Transform[num];
for (int i = 0; i < num; i++)
{
mRespPoint = gameObject.transform.GetChild(i);
}
}
public void DeletAll()
{
PlayerPrefs.DeleteAll();
for (int i = 0; i < levelList.Count; i++)
{
if (i == 0)
{
levelList*.UnLocked = 1;*
levelList*.score = 0;*
levelList*.IsInteractable = true;*
}
else
{
levelList*.UnLocked = 0;*
levelList*.score = 0;*
levelList*.IsInteractable = false;*
}
}
GameObject[] lButtons = GameObject.FindGameObjectsWithTag(“LevelButton”);
for (int i = 0; i < lButtons.Length; i++)
{
LevelButton button = lButtons*.GetComponent();*
button.Star1.SetActive(false);
button.Star2.SetActive(false);
button.Star3.SetActive(false);
if (i != 0)
{
button.unlocked = 0;
button.GetComponent().interactable = false;
}
}
}
}
why WaitForSeconds not working?

You can always DEBUG things and check it out.

  • Maybe your condition isn’t being true
  • Or maybe your OnMouseDown function isn’t being called at all

IEnumerator Wait()
{
Debug.Log("begin: " + Time.time);
yield return new WaitForSeconds (1f);
Debug.Log("end: " + Time.time);

}

begin: 2.100451
UnityEngine.Debug:Log(Object)
d__5:MoveNext() (at Assets/Scripts/props/selectedItem.cs:58)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
selectedItem:OnMouseDown() (at Assets/Scripts/props/selectedItem.cs:20)

the corroutine is not playing because the object gets destroyed, the monobehaviour cant be destroyed / disable during corroutine or it will brake after next yield, you need to start the corroutine from another monobehaviour if you must disable the object, or just disable the mesh renderer and destroy the object after the corroutine.