- Home /
Recreating adventure game dialouge system (newb)
I'm trying to recreate this old adventure game dialouge as a newb challenge, I started learning C# and unity two days ago.
I have already created a menu from which I can access a new scene where the conversation is to take place. I created four buttons and lined them up as in the old game. I created an empty on which I want to place the script and from which I want the buttons to recieve information dialouge. I have done something similar once and I'm trying to achieve a similar result again without a tutorial, except this is probably a bit more cluttery than simply changing a number in a text UI element from one variable to another.
Anyway here is what I have started with in the empty's script. A variable for each button and when you click on any of the buttons the variable number is going to change to 2 and if variable is equal to two do this to the buttons or the portrait's dialouge. But I'm getting errors already "Cannot implicitly convert type int' to
bool'" on line 24.
I'm a bit lost and wonder if someone could give me some advice. :)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DialougeScript : MonoBehaviour {
int one;
int two;
int three;
int four;
public Text text;
// Use this for initialization
void Start () {
one = 1;
two = 1;
three = 1;
four = 1;
}
public void Dialouge () {
if (one = 1){
text.text = "Hello who are you?";
if (two = 1){
text.text = "I'm lost, where are we?";
if (three = 1){
text.text = "I wish I had something to eat.";
if (four = 1){
text.text = "I've got to go I think.";
}
}
}
}
}
// Update is called once per frame
void Update () {
}
}
I'm probably going to need one function for each button right, so I can call different functions for when different buttons are being pressed?
Answer by Airman · Jan 27, 2018 at 08:06 PM
Hi there,
It seems that on your lines that contain an if statement, you're using the standard ='s sign, which is for assignment: (E.g. int A = 4 would set a variable A equal to 4.) You'll want to use the =='s sign which is for boolean expressions (E.g. if(A == 4) { text.text = "Hello world"; }.
if (one = 1){ //needs to be changed to one == 1
text.text = "Hello who are you?";
if (two = 1){ //needs to be changed to two == 1
text.text = "I'm lost, where are we?";
if (three = 1){ //needs to be changed to three == 1
text.text = "I wish I had something to eat.";
if (four = 1){ //needs to be changed to four == 1
text.text = "I've got to go I think.";
}
Answer by haroldgeronimo · Jan 27, 2018 at 08:38 PM
use double equal sign "==" on conditions one equal sign signifies variable assignment while two equal sign i used for equality conditions
ex: var i = 0; // assignment of data to variables if(i==0){ ... } // equality condition
Answer by Haugkall · Jan 27, 2018 at 08:14 PM
Thanks that fixed it. :)
Hmm yeah thanks if you don't $$anonymous$$d me asking I'm trying to change the text in the button. Am I correct in thinking the name of the text object has to be made into a variable and then changed with text.text = text1.ToString("Hello who are you?"); It's not working right now using UnityEngine; using UnityEngine.UI; using System.Collections;
public class DialougeScript : $$anonymous$$onoBehaviour {
int one;
int two;
int three;
int four;
int text1;
int text2;
int text3;
int text4;
public Text text;
// Use this for initialization
void Start () {
one = 1;
two = 1;
three = 1;
four = 1;
Dialouge();
}
public void Dialouge () {
print ("test");
if (one == 1){
text.text = text1.ToString("Hello who are you?");
if (two == 1){
text.text = "I'm lost, where are we?";
if (three == 1){
text.text = "I wish I had something to eat.";
if (four == 1){
text.text = "I've got to go I think.";
}
}
}
}
}
// Update is called once per frame
void Update () {
}
}
Hi there,
$$anonymous$$y apologies as I haven't checked the unity answer forums in a while. If you're wanting a system where you press a button and then dialogue appears, you'll need to create a button widget and provide it the functionality to do just that. I have a unity project uploaded onto my github that does this: https://github.com/RLangridge/AnswerProject.
The basic idea is that you have a script that has a set of buttons and you add a listener to the onclick list of the appropriate button. The listener would have the functionality to change the text of the speaker based on the button that you pressed.
I did this with the following code:
/// <summary>
/// We use this function to assign an onclick function to each button. As the functionality of the buttons
/// does not differ except from which index we should be using when referencing dialogue, it's better
/// to just throw in a lambda here so that we don't have to re-write a heap of code.
/// </summary>
/// <param name="_index"></param>
private void SetupOnClickFunctionsForButtons(int _index)
{
//We retrieve a button from our public button array, DialogueButtons
var btn = DialogueButtons[_index];
//We then add a listener to it, which is just a function that I've defined below.
btn.onClick.AddListener(() => OnButtonClick(_index));
}
//This function is fired off when the player clicks a button with this function as the listener (I.e the buttons that had this added as a listener above).
private void OnButtonClick(int _index)
{
//Speech text is the Text widget that I've defined for the computer and DialogueResponses is an array of strings.
SpeechText.text = DialogueResponses[_index];
}