How to use GUI.Window as you talk

I tried to use the window to show the lines and after you click the button is show the next talk, but when it ends the dialogue he’s giving an error and I don’t know how to fix.
Can you help me make it work correctly?

I put the code below to demonstrate how I did

#pragma strict

var Person:GameObject;

var windowRect :Rect = Rect (70,70,180,65);

var History:String[];

var x:int;

function Start () {

History= new String[3];
History[0]="Speak";
History[1]="Speack";
History[2]="........?!";

}

function OnGUI (){

if(Event.current.isMouse==Person){

windowRect = GUI.Window (0, windowRect, Button,History[x]);

                }
    }

function Button (windowID : int) {

     if (GUI.Button (Rect (20,30,35,30), "Next")) {
        // This code is executed when the Button is clicked
    
        x=x+1;

        if(x==3){

     GUI.DragWindow();
    }
         
    }
}

Arrays have a zero based index in most programming languages. That means that in case of x == 3 as you are writing in your code it will just blow up with a ArrayIndexOutOfBoundsException in line 24. Accessing History[3] is the culprit here so you’ll have to prevent that line from being executed if x is bigger or equal to History.Length.

Other strange code

  • Comparing Event.current.isMouse with a GameObject will never result in the behaviour you want. Did you mean to check if you clicked on this gameobject? For this you would need Camera.ScreenPointToRay and Physics.Raycast.