user8048595
user8048595

Reputation: 139

Unity-How to Create two Button events on one Dialogue Tree Script

So I've been putting together a test program with a simple dialogue tree where you get a question and two option buttons, and when you push one, you go to another level of the tree. So I made a list adding all the tree levels, containing the parameters of the question and both options. I've gotten myself confused though. I'm not really sure how go to add a level to the list if I push a certain button. Aside from an initializer script, I would like to keep it one script. The best I can think of is creating bool functions for if the button is pushed, but I can't really think of how to differentiate it between one button and the other. If anyone has any better ideas for how to go about this. I would appreciate it. Thank you.

 public class Level : MonoBehaviour {

 bool button1Pressed;
 bool button2Pressed;

private void Start()
{

    Level Level1 = new Level("Hello", "Hi", "Shut Up");
    Level leaf1 = new Level("Don't be Rude");

    Level Level2 = new Level("What you Doing?", "Not Much", "None of your Business");
    Level leaf2 = new Level("Well Excuuuuse Me");

    Level Level3 = new Level("Can I do that too?", "Sure", "Go Away");
    Level leaf3 = new Level("Fine. Be a Jerk");

    Level Level4 = new Level("This is boring, can we do something else?", "Why not?", "You're boring");
    Level leaf4 = new Level("I'll go be boring somewhere else");

    Level Level5 = new Level("You want ice cream?", "Sounds Good", "I'm allergic");
    Level leaf5 = new Level("ok.......");
    Level leaf = new Level("I Want Chocolate");      

    Level1.add(Level1);
    Level1.add(leaf1);

    Level2.add(Level3);
    Level2.add(leaf2);

    Level3.add(Level4);
    Level3.add(leaf3);

    Level4.add(Level5);
    Level4.add(leaf4);

    Level5.add(leaf5);
    Level5.add(leaf);

    levels.Add(Level1);
    levels.Add(Level2);
    levels.Add(Level3);
    levels.Add(Level4);
    levels.Add(Level5);

}



public static Text Textbox;
public static Button Button1;
public static Button Button2;

    public string OptionA;
    public string OptionB;
    public string Question;

    public string Leaf;

    private List<Level> levels;

    public Level(string question, string optionA, string optionB)
    {
        this.Question = question;
        this.OptionA = optionA;
        this.OptionB = optionB;

        Textbox.text = Question;
        Button1.GetComponentInChildren<Text>().text = OptionA;
        Button2.GetComponentInChildren<Text>().text = OptionB;

        levels = new List<Level>();

    }

    public Level(string leaf)
    {
        this.Leaf = leaf;
        Textbox.text = leaf;
    }

    public void add(Level lvl)
    {
        levels.Add(lvl);
    }

    public List<Level> getLevels()
    {
        return levels;
    }

 void OnPointerDown()
{
    button1Pressed = true;
}
void OnPointerUp()
{
    button1Pressed = false;
}

}

Initializer

public class Initializer : MonoBehaviour {

public Text Textbox;
public Button Button1;
public Button Button2;

void Awake()
{
    Level.Textbox = this.Textbox;
    Level.Button1 = this.Button1;
    Level.Button2 = this.Button2;
}
 }

Upvotes: 0

Views: 113

Answers (1)

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

You can create two public methods in your script like this

public void Button1Pressed()
{
    // Place Button 1 Logic here
}

public void Button2Pressed()
{
    // Place Button 2 Logic here
}

Then

  1. Go to each button in your scene
  2. Find the On Click () section
  3. Click the +
  4. Drag the object that contains your script with the above code into where it says None (Object)
  5. Click the dropdow saying No Function, and select your script, and the appropriate method to call, for example Button1Pressed

enter image description here

Upvotes: 1

Related Questions