Guys, I am new to unity so please explain to me as if I were stupid. =D
I am trying to make a series of lines of text appear on screen (canvas) and I want the user to be able to go through them by clicking “Enter” (Submit) every time.
I am having problems with my code. Probably, there is an easier way of doing this but what I have right now is this and it isn’t working =(
import UnityEngine.UI;
var messageText : Text;
var questOneCompleted : int = 0;
var questOneMessagesShown : int = 0;
function Start () {
}
function Update () {
if (questOneCompleted == 0) {
//start of quest 001 (Look Around)
if (questOneMessagesShown == 0) {
messageText.text = "message 1";
questOneMessagesShown = 1;
}
if (Input.GetButtonDown("Submit")) {
if (questOneMessagesShown == 1) {
messageText.text = "message 2";
questOneMessagesShown = 2;
}
if (Input.GetButtonDown("Submit")) {
if (questOneMessagesShown == 2) {
messageText.text = "message 3";
questOneCompleted = 1;
}
}
}
}
}
Also I am not sure how to include multiple "if"s in my script.
If you’re new to Unity, you should be learning C#. UnityScript (every time someone incorrectly calls it JavaScript, a kitten dies) is losing support and isn’t worth learning when there’s a much more popular C# option. If you spend that time learning C#, you’ll know a powerful language that can be used for many things (WPF, Windows Forms), and it’s pretty close to Java, which is also very popular.
As for multiple IF statements, it would look like this:
function Update()
{
if (questOneCompleted == 0)
{
//do stuff
}
if (questTwoCompleted == 0)
{
//do stuff
}
if (questThreeCompleted == 0)
{
//do stuff
}
}
There’s no difference if you write this:
void Function(){
Debug.Log("Works!");
}
Or this:
void Function()
{
Debug.Log("Works!");
}
However, I prefer the 2nd method as I think it’s easier to read and keep track of where the brackets start and end.
There are two ways to approach it. One was mentioned by @DroidifyDevs . There is a potential downside to this method though. Each of them will run because the previous one was changing the value that was being checked.
if (questMessageShown == 0) {
questMessageShown = 1;
}
if (questMessageShown == 1) {
questMessageShown = 2;
}
if (questMessageShown == 2) {
questMessageShown = 3;
}
You could solve it by flipping them.
if (questMessageShown == 2) {
questMessageShown = 3;
}
if (questMessageShown == 1) {
questMessageShown = 2;
}
if (questMessageShown == 0) {
questMessageShown = 1;
}
Alternatively you could solve if by using an else statement with the if statement. This is the second method of having multiple if statements. In this example only one of them will run despite the value being changed.
if (questMessageShown == 0) {
questMessageShown = 1;
}
else if (questMessageShown == 1) {
questMessageShown = 2;
}
else if (questMessageShown == 2) {
questMessageShown = 3;
}
An alternative to both of these would be to use a switch statement. In this example only one will run.
switch (questMessageShown) {
case 0:
questMessageShown = 1;
break;
case 1:
questMessageShown = 2;
break;
case 2:
questMessageShown = 3;
break;
}
However in this example the first one will run followed immediately by the second one, but the third will only run by itself.
switch (questMessageShown) {
case 0:
questMessageShown = 1;
case 1:
questMessageShown = 2;
break;
case 2:
questMessageShown = 3;
break;
}
If the switch statement seems confusing feel free to ignore it for now. You’ll eventually cover it.
It’s gone beyond losing support now. It’s literally marked by Unity for future removal. Eventually you’ll have no choice.
I try to avoid many IFs in a row like the plague. It’s very easy to use many IF statements in a row for basic logic from a newbie perspective, but if you have 4+ IF statements in a row and there is no very specific reason as to why you need them, it’s a sign you don’t know exactly what you’re doing. I still sometimes do that, but I think I’ve gotten it more-or-less under control compared to what I was doing even a few months ago.
if (Input.GetButtonDown("Submit")) {
if (questOneMessagesShown == 1) {
messageText.text = "message 2";
questOneMessagesShown = 2;
}
if (Input.GetButtonDown("Submit")) {
if (questOneMessagesShown == 2) {
messageText.text = "message 3";
questOneCompleted = 1;
}
}
So at this point your edit is pretty much mandatory.
As you pointed out, @OP’s code does have that pattern going (I’m guessing by the name “questOneMessagesShown " and his frequent use of " = 2, = 3” instead of ++), so in the long run I’d rewrite that script so it scales well with bigger projects.
In fact, why the hell is he hard-coding the messages in the first place? They should be in an array so he can do this:
if (questOneMessagesShown == 1) {
MessageArray[questOneMessagesShown];
questOneMessagesShown++;
}
if (Input.GetButtonDown("Submit") && questOneMessagesShown < MessageArray.Length)
{
MessageArray[questOneMessagesShown];
questOneMessagesShown++;
}
When it comes to multiple languages or long dialogues, is OP going to have a 10,000 line long script?