Invoke with parameters?

I’m doing a dialogue manager, and it works except for a polish thing, which is that the text starts playing without waiting for the text box to appear (it takes around 0.25 seconds for the text box to appear, which is not much, but for a slight moment, the text can be seen written in the air because they start at the same time). I tried putting the text box appearance and the dialogue starting as different methods, and then calling an Invoke to make the dialogue wait a little (around 0.4 seconds, just to be sure the text Box is there). The problem is that I can’t Invoke the method beacuse it has a parameter with the data for my dialogue

//This is my DialogueManager Script

public void TextBoxAppear()
    {
        boxAnimation.SetBool("isOpen", true);
    }

    public void StartDialogue (Dialogue dialogue)
    {
        //Code to show the dialogue
    }

I’m invoking it from a trigger script, like this

public void Dialogue()
    {     
        dialogueStart = true;
        FindObjectOfType<DialogueManager>().TextBoxAppear();
        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
    }

I tried changing the Start Dialogue into an Invoke, like this:

public void Dialogue()
    {     
        dialogueStart = true;
        FindObjectOfType<DialogueManager>().TextBoxAppear();
        FindObjectOfType<DialogueManager>().Invoke ("StartDialogue(dialogue)", 0.4f);
    }

But it doesn’t work. Is there some place to place the parameter outside of the string or is there some way around it? I saw a post that recommended using Coroutines and using a WaitForSeconds, but I don’t know if that could work since I’m calling the Coroutine from another Script.

I would appreciate any help

I tried that, making a Coroutine to add a waiting time, but since I’m calling it from another script, it tells me that my method (StartDialogue) “doesn’t exist in the current context”

public void Dialogue()
    {     
        dialogueStart = true;
        FindObjectOfType<DialogueManager>().TextBoxAppear();
        FindObjectOfType<DialogueManager>().StartCoroutine(StartDialogue(dialogue));
    }

Hmmm, maybe try storing it in a variable. This is an example of how i’ve called a Coroutine from another script:

PlayerHealth playerScript = other.gameObject.GetComponent<PlayerHealth>();
            StartCoroutine(playerScript.InvulnFlash());

Note that the other gameobject comes from an OnTriggerEnter2D function but i think a similar format should get you results.

I tried storing the component in a variable

public DialogueManager dialogueManager;

private void Start()
    {
        dialogueManager = FindObjectOfType<DialogueManager>();
    }

    public void Dialogue()
    {     
        dialogueStart = true;
        dialogueManager.TextBoxAppear();
        dialogueManager.StartCoroutine(StartDialogue(dialogue));
    }

But still it drops an error, is there something I did wrong?

Is it the same error as above?

Yeah, the same that the Coroutine “StartDialogue” doesn’t exist in the current context

Try this: StartCoroutine(dialogueManager.StartDialogue(dialogue));

1 Like

Yeah, that worked! Huh, I’ll keep in mind to use that syntax next time. Thank you very much!

Nice, glad it worked!

1 Like