- Home /
Question by
bobmcdonal · May 13, 2021 at 12:02 AM ·
c#scripting problemmovementrigidbody2dwhile
Disabling script while in certain position
I'm trying to disable my movement script while the player is near the top of the screen. Currently my code is just disabling the script once I get into position and then never enabling itself once gravity takes me below the disabled area.
while(rigidbody2d.position.y <= 5 && rigidbody2d.position.y >= 4.2)
{
this.enabled = false;
break;
}
this is part of the Update method attached to my player object. Not sure what I need to change or add so that it enables itself.
Comment
Answer by _samuel12345 · May 13, 2021 at 12:34 AM
Once a script is disabled it stops running, so it looks like the problem is that it disables itself but will never check if your player is in a position in which it can be reactivated. You should enable/disable this script from another script, something like this:
while(rigidbody2d.position.y <= 5 && rigidbody2d.position.y >= 4.2)
{
gameObject.GetComponent<ScriptToDisable>().enabled = false;
break;
}