- Home /
WaitForSeconds
hi, i used a code for my buttons to load my 99 levels, but i can't added WaitForSeconds to wait before loading a level, when i did it on a function my code bug.
any suggestion. Thanks
public var sound1 : AudioClip;
public var sound2 : AudioClip;
private var i = 1;
private var s : int;
function Update ()
{
for (i=1; i<99; i++)
{
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider.name == "button"+i)
{
audio.PlayOneShot(sound1);
s = i;
}
}
if (Input.GetMouseButtonUp(0))
{
if (s == i)
{
s = 0;
audio.PlayOneShot(sound2);
Application.LoadLevel ("Level "+i);
}
}
}
}
No. Seeing as how you haven shown us either the code that actually failed, nor the error message spruced, none is likely to be able to help you.
As Such, this comes down to "write my code for me", something we don;t do here. If thats what you want I suggest you try the forums.
Answer by cryingwolf85 · May 20, 2014 at 07:06 PM
Sounds like you need to look over some Unity Scripting Basics.
Your problem sounds like a job for the Invoke method. Use it like this:
// Will call the method LoadLevel after 5 seconds
Invoke("LoadLevel", 5);
Thanks for the answer, but it seems to do the same thing as WaitForSeconds. Booth work good, and i tried WaitForSeconds in other script and i works great, but in this case, i have to press button 1 ("button "+i) and load level 1 ("Level "+i), when i use WaitForSeconds or Invoke, they don't do that. i think i should use them other way, but i don't know how. If you understand my problem, hope you have an other solution or the right way to use it.
The way that you are going about this is wrong.
Problem 1: You trapped in a giant for loop. This is bad bad bad.
Problem 2: Don't set s = to i at all in this manner. Very messy. Find a more elegant solution if you continue this way.
Problem 3: Just the way that you are doing the GUI. Look at OnGUI. It's what you need for the buttons.
I Highly suggest you referring to the link I sent you in my previous comment. One read through this and you will be able to find solutions to simple problems much more easily.
Thanks for the advise, i'm beginner on unity and code so if you have a suggestion to di this code better, you are welcome.
Those were my suggestions. I really do suggest your reading through the links, they will make your code better. I can't write your code for you, only you can!