I'm trying to move an object without Update().... Not successing so far.

Hi.
I hope this is the right place to ask, this is my first question.
First of anything, this is my code:

public class Bot_Ins_Menu  : MonoBehaviour
{
    public static bool menuActive = false;
    public static bool isPaused = false;


    public GameObject botMenuUI;

    public GameObject robot;
    public List<string> funcs = new List<string>();

    //moving block
    public float forceMult = 200;
    private Rigidbody robotRB;
   
    void Awake() {
    robot = GameObject.Find("CubeRobot");
    robotRB = robot.GetComponent<Rigidbody>();

    //funcs.Add("Move10Units");
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && menuActive == false)
        {
         Time.timeScale = 0f;
         Cursor.visible = true;
         botMenuUI.SetActive(true);
         menuActive = true;

        } else if (Input.GetKeyDown(KeyCode.E) && menuActive == true)  {
            Time.timeScale = 1f;
            Cursor.visible = false;
            botMenuUI.SetActive(false);
            menuActive = false;
        }



    }
    void AddToStack(string a) {
        funcs.Add(a);
    }

    public void Move10UnitsForw() {
        //robotRB.velocity = transform.forward * Time.deltaTime * forceMult;
        robot.transform.position += transform.forward * Time.deltaTime;

    }

    public void Jump() {
        robot.transform.Translate(0,10,0);
    }

    public void Move10UnitsBack() {
        robotRB.velocity = transform.forward * -1 * Time.deltaTime * forceMult;
    }

    public void RunTheStack() {
        foreach (string a in funcs) {
            Invoke(a, 1);
        }
        funcs = new List<string>();
    }

}

I have a List called “funcs” which I’m trying to run every “function” in it when the user clicks on “Run The Stack” button. Which button calls “RunTheStack” function as you can also see in the code.

For example, if the funcs List has “Move10UnitsForw” function in it I want it to run the Move10UnitsForw function. The problem is I can’t call that function every frame unless it’s in Update(). But I also can’t do simply put it in Update() because I want it to be run for just a little. Say, go a little bit and then stop.

Thanks for any help.

These things are broadly called “runners” or “task runners” or coroutine runners if you like.

Why don’t you just start coroutines through Unity to do what you need? If you need to control a task list of things to do, just make a list of IEnumerators and then send them off to the coroutine runner one at a time.

You can also pump a coroutine object yourself with .MoveNext()…

Coroutines in a nutshell:

Splitting up larger tasks in coroutines:

Coroutines are NOT always an appropriate solution: know when to use them!

2 Likes

Thank you so much. I will keep using this approach. The only problem I have is to get a list of coroutines from my “slot” objects.