- Home /
Beginner needs help with a scripting question please
Can anyone show me how to get this script working with the "commented out" lines included. It works as it is but I can't manage to get it to work with a delay before loading the level.
I've tried different ways to set the script out, I'm still only a beginner.
var StartTime : float;
var customSkin:GUISkin;
function OnGUI (){
if(GUI.Button(Rect(0,0,100,50),"Play Game")){
// StartTime = Time.time + 3;
// if (Time.time-StartTime >= 3);{
Application.LoadLevel ("game") ;
}
}
Answer by Berenger · Jun 06, 2012 at 03:37 AM
The code inside if(GUI.Button(...)) will be executed only at the exact frame you release the mouse at the end of the clic. If you want to wait, you must do it somewhere else.
There is several way. The simplest is to use Invoke, with 3 as the second parameter. Declare a function that only call LoadLevel and give it's name as a string for parameter one. Like this :
function OnGUI(){
if(GUI.Button(Rect(0,0,100,50),"Play Game")){
Invoke( "WaitAndLoad", 3.0 );
}
}
// You can choose another name, but it must match with the one in Invoke
function WaitAndLoad(){
Application.LoadLevel ("game") ;
}
There is another way, just a tiny bit more complicated. For now stick with this one, and keep the words coroutine and yield in a corner of your mind for when you feel more comfortable.
@Rob333: First, don't post this as answer! Answers should answer the question. I've converted your answer into a comment.
Second, you don't have enough karma to directly post to this site. Your posted will be placed into the moderation queue. Every user with 1000+ karma can publish your posts, but you have to wait until someone can find the time to do so. Don't post the same thing multiple times.
Thanks for the help Berenger, your script worked in my test project but still can't make it work in my main project. I've just posted a new question with details of what's happened, tile of question is "Unusual problem loading a scene".
Your answer
Follow this Question
Related Questions
Using Time with OnGUI Help 2 Answers
Time.time button delay trick causing NaN 0 Answers
How to add delay before showing GUI Button? 3 Answers
Clickable Scrollable List? 1 Answer
MouseOver GUI.Label Information 1 Answer