- Home /
When i call a function, it doesn't run and stops the script
I'm trying to call a function in another script, but when i call it it doesn't run it at all and the script stops and exit. I got no errors. I tried debugging and it works fine till i call the function in the script, which is not executed and the script stops completely.
if(currentscene == "Level02")
{
unlock.hasUnlocked("Level02"); //this is the last line of code which the scripts executes
Invoke("Level03", 2f);
}
public void hasUnlocked(string level)
{
if (level == "Level02")
{
Level02.enabled = true;
}
Answer by ray2yar · Jul 08, 2020 at 02:31 AM
If I understood your logic.... you say IF you're in the level2.... THEN unlock level2... which loads level2.... so, wouldn't that result in continuously loading level2 ?
No, if i am in level 2 and i completed it, i unlock the level 2 button on the level selector scene, which is why i have unlocked.hasUnlocked("Level02"). After that is done i invoke the Level3 scene. $$anonymous$$y problem is that is stops working when it does the line unlocked.hasUnlocked("Level02").
Answer by Tripleganger · Jul 09, 2020 at 10:00 AM
Could I suggest you used this to avoid any error? Please also consider that Level02.enabled will enable the Button Component in your scene, if that is what you are trying to do.
using UnityEngine.SceneManagement;
public void HasUnlocked(string level)
{
Scene myScene = SceneManager.GetActiveScene();
if (myScene.name == level) Level02.enabled = true;
}
Yeah i'll try it ty, but idk if this is going to solve the main issue, cause HasUnlocked is not even executed. It all stops when i call "unlock.hasUnlocked("Level02");"
If hasUnlocked is not called, it means that something is wrong in this part of the script: if(currentscene == "Level02") { unlock.hasUnlocked("Level02");
Invoke("Level03", 2f);
}
Bear in $$anonymous$$d that if this is in Update, you are calling Invoke every frame. You might want to insert a bool that prevents this from happening. I don't know if this will fix the problem, but it might be worth trying.
currentscene = Scene$$anonymous$$anager.GetActiveScene().name;
if (currentscene == "Level01")
{
Invoke("Level02", 2f);
goto Finish;
}
if(currentscene == "Level02")
{
unlock.hasUnlocked("Level02");
Invoke("Level03", 2f);
goto Finish;
}
if (currentscene == "Level03")
{
Invoke("Level04", 2f);
unlock.hasUnlocked("Level03");
goto Finish;
}
i have smt like this. this is called when i complete the level, it's not in Update. I really don't know why this is happening
Your answer
Follow this Question
Related Questions
Unreliable particle behaviour when not in view 0 Answers
Why is my character animation not working ? 1 Answer
Static batching not working... 0 Answers
No reason why this shouldent work 0 Answers
two OnMouseDowns are not working at a time. -2 Answers