I having a little problem with my code that make feel like a idiot…
What im trying to do here with my code is a menu. I i press positive Vertical Debug the position where it is, same with the negative vertical.
Now, my problem started with the velocity in that my vertical control is moving so i decided to delay it with yield WaitForSeconds but looks like is not working or stg is wrong with my code.
private var position = 1 ;
function Update () {
if(Input.GetAxis("Vertical")<0)
{
if(position == 4)
{
position = 1;
Menu();
}
else{
position += 1;
Menu();
}
}
else if(Input.GetAxis("Vertical")>0)
{
if(position==0)
{
position = 3;
Menu();
}
else
{
position -= 1;
Menu();
}
}
}
function Menu()
{
switch(position) {
case 1:
Debug.Log("Arcade");
Wait();
break;
case 2:
Debug.Log("2 Players");
Wait();
break;
case 3:
Debug.Log("Network");
Wait();
}
}
function Wait()
{
yield WaitForSeconds(5);
}
Any other idea to help me fix my problem will be welcome
Your code does wait, you’re simply constantly calling the same subroutines on each frame : Update → Menu → Wait. Without some sort of boolean flags controlling the routine calls, you won’t get the desired result.
Instead of using yield to control the speed, expose your position incremental values as variables on the global level, multiply them by Time.deltaTime to become frame rate independent and tweak the values until you get he desired speed.
if you are trying to put some kind of delay instead why not try making the button that you are trying to delay less responsive? or not so sentitive? like going to edit > project settings>input and find the one you want to mess with.
somethings don’t always have to be done in code.
Most probable cause is that the objects b1b and b1g aren’t instantiated when you call them. Instead of trying to make use of them as soon as you find them, store them in a variable and check if they exist before you activate / deactivate them :
var thisObject : GameObject = GameObject.Find("b1b");
if(thisObject) thisObject.SetActiveRecursively(false);
This way you protect yourself from null reference exceptions.
Why not use InvokeRepeating to set how often you wish to call some code?
Unity - Scripting API: MonoBehaviour.InvokeRepeating - it has a second parameter to specify WHEN to start calling it, for example 5 seconds delay before it will start calling, and a 3rd parameter to specify how often to call it.