[SOLVED] JavaScript - "Submit" button in script

Guys, I am quite new to unity so please explain this as if I’m stupid.
Please do not post C# replies as I need JS only.

Basically, I want a series of messages to be displayed on a Canvas and to be able to go through them by clicking Enter (Submit)

import UnityEngine.UI;

var messageText : Text;
var questOneCompleted : int = 0;
var questOneMessagesShown : int = 0;

function Start () {
}

function Update () {
    if (questOneCompleted == 0) {
            if (questOneMessagesShown == 0) {
            messageText.text = "my message one";
            questOneMessagesShown = 1;
            }
    if (Input.GetButtonDown("Submit")) {
        if (questOneMessagesShown == 1) {
            messageText.text = "different message";
            questOneMessagesShown = 2;
        }

            if (Input.GetButtonDown("Submit")) {
        if (questOneMessagesShown == 2) {
                    messageText.text = "third line";
                    questOneCompleted = 1;
        }
        }

    }
    }
    }

Yeah I know this isn’t a very “professional” block of code so please help correct it. It doesn’t work btw. After the first submit it skips straight to the third message.

I had the same problem once and i used this script to switch between sentece

if(Input.GetMouseButtonDown(0) && sentence <6){
    seentece +=1;

}
if (sentence == 1){
dialogue = "text 1";
}
if (sentence == 2){
dialogue = "text 2";
}

when ever in this case you click you mouse button it will go to the next line of text

1 Like

JavaScript? Please?

I will try this in JavaScript. Wait 10 min

Yeah the code is java

if(Input.GetMouseButtonDown(0) &&  //how many lines you want// sentence <6){
    seentece +=1;

}
if (sentence == 1){
dialogue = "text 1";
}
if (sentence == 2){
dialogue = "text 2";
}

accidentally i click the c# code spoiler

1 Like

One quick note you have to set a start varribale for what you begin text is
and with line of text you want to use in this case the first sentence

var sentence = "1";
var dialogue = "";

It works! you are officially my hero. =D

Nice

Good luck on your project

var sentences = int : 0;
var messageText : Text;

function Start () {
}

function Update () {

if (GetButtonDown("Submit") {
sentences +=1
}

if (sentences == 1) {
messageText.text = "script works";
}
if (sentences == 2) {
messageText.text = "definitely works";
}
}

When connect code to an object in hierarchy, set the Text parameter by dragging and dropping Text from canvas to the field.

Technically that code sample is perfectly valid in both languages. :smile:

2 Likes