- Home /
Button to new scene
Hi,
I have a problem where I can't seem to get my java code to work to switch to a new scene. I have the code linked to the "OnClick" event of the button.
The code it:
#pragma strict
function LoadScene ()
{
Application.LoadLevel("MainGame");
}
However, when I go into the function select, next to "Runtime only", the LoadScene function is not there, as if it is not detecting the function.
How do I fix this?
Thanks Daniel Colthart
$$anonymous$$aybe you forgot to save the script? or you picked the wrong object to pick the script from?
$$anonymous$$aybe, I have just gone into unity, and now I can only select No Function or $$anonymous$$onoscrip -> string name
Answer by DcoltGaming · Sep 25, 2016 at 01:43 PM
Restarting and re-adding the script to an empty got it to work.
Answer by Damastasam · Sep 23, 2016 at 07:09 PM
You may need to write some code that calls the LoadScene function. Like an if statment
if (do somthing) {
LoadScene()
}
else if (do somthingelse) {
}
Answer by aditya007 · Sep 25, 2016 at 01:43 PM
Hi @DcoltGaming I don't really know about java, but in C# you need to make the function "public" for it to be visible in the methods list.So, check whether you need to do it in java or not. And also check again whether you have assigned the gameObject, on whom this script is attached, below the "Runtime Ony".
Hi $$anonymous$$ate,
All sorted now. I restarted and reassigned to a new empty and it worked. Thanks for your answer though.
Your answer
Follow this Question
Related Questions
Using UI.Button to Load Another Scene? (using outdated tutorial video) 1 Answer
buttons not being able to be clicked? 1 Answer
Load and run scene in background 1 Answer
Only one scene? 2 Answers