Adding Buttons to the Text Adventure

I tried to imitate what I did with the sprite, but the new serializefielded image doesn’t accept anything.
Before I tried

public Button getButton()
{return button;}

But then in the AdventureGame script I couldn’t figure out what to add after buttonComponent. = state.getButton();

Any help would be very appreciated!

Hi Asli,

What exactly is your goal? What is button supposed to be? At the moment, it seems to be a component of type Image. As far as I know, you cannot assign any game objects from your Hierarchy to an object in your Assets folder.

Hi Nina!

I want to add buttons to each State but some States will have one button and some will have 2, so I want to be able to control them via my code. I have no idea how though :smiley:

What exactly do you mean by button? Button like a clickable UI button? If so, you could create an array of buttons in your scene and reference them. Are you a bit familiar with arrays?

In your Hierarchy:

Buttons (parent)
- Button child   // [0]
- Button child   // [1]
- Button child   // [2]
- Button child   // [3]

When you have a reference to the “Buttons” parent game object in your AdventureGame instance, you can access its children via GetChild(i) where i is the index. The first element is at index 0.

Then you could map the buttons to your nextStates array. You know the number of elements in your nextStates array, so you could iterate over the children of the “Buttons” game object and enable the children you need.

The tricky part is to have the AdventureGame instance know which button was clicked, so you want to pass on an integer which you could use in the nextStates array. I’m wondering if you could expose a parameter in the OnClick() field in the Unity Inspector. Then it would be very simple to load next states on button click. Here is a brief tutorial.

As you can see, the topic is fairly complex. Maybe my ideas are too complicated, and there is a simpler solution, so it would be great if you could explain yours. Accessing a Button is not the problem. I know you can do that because you did something similar with Image. The main problem is to make that Button do something.

Oh, I’m familiar with arrays but this is a bit too complicated for me. I’ll try to do it if there is no other option, but I was thinking of implementing the KeyCode solution Rick gives and just replacing that with OnMouseDown. That wouldn’t work?

Would this work if I gave up on States and just did different Scenes, instead?

Hi Nina! I decided to forgo buttons for now :smiley: Thanks so much for your help!

You are welcome. :slight_smile:

In the Number Wizard UI section, you’ll learn about buttons, and in the following sections a bit more about arrays. You could go back to your Text Adventure at a later juncture and implement your idea. It’s not that difficult but the solution will probably require a few more steps than displaying an image. :slight_smile:

OnMouseDown could be a part of the solution. Basically, with your current knowledge, you could already write a big part of the solution. The remaining problem would be to distinguish between the buttons during runtime because you want to “load” the correct state on button click.

Rick mapped his states to keys. Alpha1 “loads” state[0], Alpha2 state[1], and so on. The keys are already there. The buttons aren’t, and if they are, you probably don’t want to display buttons that are not supposed to do anything.


If you have the same number of next states in your State objects, you could hard-code a solution. That is very simple because you could connect a button with a public method in AdventureGame. For example:

public void LoadState0()
{
    State[] nextStates = state.GetNextStates();
    state = nextStates[0];
}

public void LoadState1()
{
    State[] nextStates = state.GetNextStates();
    state = nextStates[1];
}

// and so on

As you can see, the code is repetitive. Repetitions usually indicate that the code could be refactored. Hard-coding stuff is usually not the best approach. Ideally, you want to achieve something that is as dynamic as Rick’s approach. Nevertheless, if this works it is a solution by definition.

I was thinking of using onMouseDown! The problem is, some pages will have 1 button, and some will have 2 (some pages will only have the Continue button).

I tried putting an image and doing an OnMouseDown function but VS won’t let me declare it as a public void so I cannot assign it to the image. (Thank you so so much for your help btw.)

How are you getting on with this, @Asli_Tumerkan? Have you already tested something else or the approach I suggested?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms