- Home /
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[i].name)
{
gm.rightAnswer++;
mustDestroyed = i;
Destroy(gm.clonImg[i]);
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<Level> 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[i].position, Quaternion.identity) as GameObject;
LevelButton button = newButton.GetComponent<LevelButton>();
button.LevelText.text = levelList[i].LevelText;
if (PlayerPrefs.GetInt("Level" + button.LevelText.text) == 1)
{
levelList[i].UnLocked = 1;
levelList[i].IsInteractable = true;
}
levelList[i].score = PlayerPrefs.GetInt("Level" + button.LevelText.text + "_score");
if (levelList[i].score > 0)
{
button.Star1.SetActive(true);
}
if (levelList[i].score > 1)
{
button.Star2.SetActive(true);
}
if (levelList[i].score > 2)
{
button.Star3.SetActive(true);
}
allStars += levelList[i].score;
button.unlocked = levelList[i].UnLocked;
button.GetComponent<Button>().interactable = levelList[i].IsInteractable;
button.GetComponent<Button>().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<LevelButton>();
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[i] = gameObject.transform.GetChild(i);
}
}
public void DeletAll()
{
PlayerPrefs.DeleteAll();
for (int i = 0; i < levelList.Count; i++)
{
if (i == 0)
{
levelList[i].UnLocked = 1;
levelList[i].score = 0;
levelList[i].IsInteractable = true;
}
else
{
levelList[i].UnLocked = 0;
levelList[i].score = 0;
levelList[i].IsInteractable = false;
}
}
GameObject[] lButtons = GameObject.FindGameObjectsWithTag("LevelButton");
for (int i = 0; i < lButtons.Length; i++)
{
LevelButton button = lButtons[i].GetComponent<LevelButton>();
button.Star1.SetActive(false);
button.Star2.SetActive(false);
button.Star3.SetActive(false);
if (i != 0)
{
button.unlocked = 0;
button.GetComponent<Button>().interactable = false;
}
}
}
} why WaitForSeconds not working?
can you put a debug.log and make sure the corroytine is starting? is the object with that script attached disabled?
Answer by xxmariofer · Mar 12, 2019 at 08:14 PM
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.
Answer by SheZii · Mar 12, 2019 at 08:51 AM
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
Answer by Apoffis · Mar 12, 2019 at 07:38 PM
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)
since i imagine you didnt read my comment ill ask again, are you making the object who has that script attached disable?
the script is attached to the object, which is deleted after the click, and the quarantine should work after a few destroyed objects
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Resources bar in every scene 2 Answers
Why Is My Reset Button for my high score ? Here is my copied script. 0 Answers