c# waitforseconds

hi all

my code is:

void Update () {
        
        ThirdPersonController playerController = GetComponent<ThirdPersonController>();
        float currentSpeed = playerController.GetSpeed();
        if (currentSpeed > playerController.walkSpeed)
        {
            animation.CrossFade("walk");
        }
        else if (currentSpeed > 0.1)
        {
            animation.CrossFade("walk");            
        }
        // Fade out walk and run
        else
        {
            animation.Blend("walk", 0.0f, 0.3f);
        }
        animation["walk"].normalizedSpeed = walkSpeedScale;

        if (Input.GetMouseButtonDown(0))
        {
            busy = true;
            animation.CrossFade("attack");
        }

        if (Input.GetMouseButtonDown(2))
        {
            busy = true;
            animation.CrossFade("shielded");
        }

        waitForSeconds();
                    

	}


    IEnumerator waitForSeconds()
    {
        yield return new WaitForSeconds(10f);

    }

but this dont wait for 10 second.
i want if player doing attack then it cant do attack till 10 seconds.
how can i do that?

make Update an IEnumerator returning instead of void.

normally what you would do is in an IEnumerator function use

yield return bla;

where bla; is the new WaitForSeconds or StartCoroutine(anotherCoroutine())

that way it will wait with the execution of the stuff after

either way this is a very bad idea, don’t use update.
instead use an own coroutine that you execute in start.

reason is that update will be called once per frame, no matter if you wait or not → MASSIVE AMOUNT OF FUNCTIONS RUNNING IN PARALLEL

        if (Input.GetMouseButtonDown(0)  busy == false)
        {
            busy = true;
            animation.CrossFade("attack");
            waitForSeconds();
        }

        if (Input.GetMouseButtonDown(2)  busy == false)
        {
            busy = true;
            animation.CrossFade("shielded");
            waitForSeconds();
        }


                   

   }


    IEnumerator waitForSeconds()
    {
        yield new WaitForSeconds(10f);
        busy = false;
    }

Note : I simply changed some lines, following logic. If it do not work because of syntax, I don’t know !

It won’t work. In C# you need to use StartCoroutine to run a coroutine or the function will simply not be called at all. Also, yielding in Update is simply not possible, so starting the wait function from it will not have any wait effect at all (as it will only actually wait if you yield the result of StartCoroutine).

hi all
i used IEnumerator update(){…}
but error:
Script error: Update() can not be a coroutine.

i used say of akilae trib but dont wait.i correct syntax but dont work.

how can i wait for seconds and do my work?
thankful.

As the error says, you’ll need to move away from using Update for this. You could use Start instead:

IEnumerator Start()
{
    ThirdPersonController playerController = GetComponent<ThirdPersonController>();

    while (true)
    {

        float currentSpeed = playerController.GetSpeed();
        if (currentSpeed > playerController.walkSpeed)
        {
            animation.CrossFade("walk");
        }
        else if (currentSpeed > 0.1)
        {
            animation.CrossFade("walk");            
        }
        // Fade out walk and run
        else
        {
            animation.Blend("walk", 0.0f, 0.3f);
        }
        animation["walk"].normalizedSpeed = walkSpeedScale;

        if (Input.GetMouseButtonDown(0))
        {
            busy = true;
            animation.CrossFade("attack");
        }

        if (Input.GetMouseButtonDown(2))
        {
            busy = true;
            animation.CrossFade("shielded");
        }

        yield return new WaitForSeconds(10f);
   }           
}

This is your original function. I doubt it will do what you want though, as waiting 10 seconds between every input makes for an unplayable game.

I think I will avoid any topic with C# code from now (out of competence), I only say more useless stuff than usual… :?

using JS does not change anything to the problems with usability and making sense though.
the syntax is different, the restrictions though are equal

hi all
1-
my enemyDamage script that must decrease hitPoint is:

 void applyDamage(int damage)
    {
        if (!die  busy==false)
        {
            hitPoint -= damage;
            busy = true;
            //if (hitPoint <= 0)
            //die = true;
            waitForSeconds();
            print(hitPoint);
        }
        else if(die)
            Destroy(gameObject);
       
    }

    IEnumerator waitForSeconds()
    {
        busy = false;
        print(busy);
        yield return new WaitForSeconds(3f);
    }

but i think it dont enter waitForSecond() function
becouse it dont print busy.
what is it problem?

2-
i want with each sword attack only one damage hit to enemy but now many damage hit to enemy with one attack.
thankful.

hi all
my problem solved.
thankful.