#I want to disable script and then after 2 seconds enable it

1 messages · Page 1 of 1 (latest)

wild zodiac
vast vortex
#
collision.gameObject.GetComponent<EnemyMovement>().enabled = false;
StartCoroutine(slide());
collision.gameObject.GetComponent<EnemyMovement>().enabled = true;

you'll probably need the logic within the coroutine

#

this code all runs on a single frame..

#

turns off the object, starts the coroutine, turns on the gameobject you'll never notice it being off

wild zodiac
#

can I put Ienumerator in the oncollisionenter

#

or create a function inside oncollisionenter and then call in coroutine

vast vortex
#

no.. u have to call the coroutine

#

and im not sure parts of me think the coroutine would continue but parts of me think it wouldnt

#

in situations like this i do 1 of 2 things

  • i have some other script turn the thing on and off (like a parent object) with the one ur modifying as a child..
  • i disable and enable the components on the script, rather than the actual gameobject
#

so for a Player for example id disable the Character Controller, Graphics, and Player Inputs.. and then re-enable them. (this allows the script to continue to run b/c we're not disabling the entire gameobject)

#

Disabling the script won't automatically halt any coroutines that were started by it. Keep in mind that if your coroutine is responsible for re-enabling the GameObject at the end, you should ensure that the GameObject doesn't get re-enabled while the script itself is disabled, as this could lead to unexpected behavior.

#

looks like u can do it that way

#

but u'd need to start the coroutine b4 u disabled..

wild zodiac
#
            collision.gameObject.GetComponent<EnemyMovement>().enabled = false;
            
            collision.gameObject.GetComponent<EnemyMovement>().enabled = true;```so is this the thing you say
vast vortex
#

no, what i meant is

public class MyTriggerScript : MonoBehaviour
{
    private EnemyMovement myEnemyScript;

    private void OnTriggerEnter(Collider other)
    {
        myEnemyScript = other.gameObject.GetComponent<EnemyMovement>();
        myEnemyScript.enabled = false;
        StartCoroutine(SlideCoroutine());
    }

    private IEnumerator SlideCoroutine()
    {
        // Your sliding logic here
        // ...
        // ...

        yield return new WaitForSeconds(1.0f);

        myEnemyScript.enabled = true;
    }
}```
#

or u could disable it at the start of the coroutine and disable it at the end.. isntead of breaking it up with the disable in the trigger and the enable in the coroutine..

#
 private void OnTriggerEnter(Collider other)
    {
        EnemyMovement enemyScript = other.gameObject.GetComponent<EnemyMovement>();
        StartCoroutine(SlideCoroutine(enemyScript));
    }

    private IEnumerator SlideCoroutine(EnemyMovement enemyScript)
    {
        // Disable the EnemyMovement script
        enemyScript.enabled = false;

        // Your sliding logic here
        // ...
        // ...

        yield return new WaitForSeconds(1.0f);

        // Re-enable the EnemyMovement script
        enemyScript.enabled = true;
    }```

in this example you can pass in the enemymovement script directly into the coroutine that way u dont need the extra variable
wild zodiac
#

nothing changed as I can see

#

enemy movement script

#

I think we can just use wait coroutine in enemymovement

#

I think we can use EnemyStates here