Magic Conch with a twist

I’m making a game for a final project and I’m not really good with coding. All locked doors will be locked with a magic conch lock. I need a script to when “E” is pressed on the object, it opens a dialog box asking if you want to spend X amount to ask the conch to open the door. I need it to have a 45% chance of saying yes and 55% of saying no. Each option plays the corresponding sound. If it says no it closes the dialog box but if it says yes it closes it and destroys the parent object.

It would be greatly appreciated to get some starting code or such.

You will need to look into triggers for the e prompt.

Look into Random.Range for the chance of yes or no.

For the popup you can simply turn a ui on and off.

Look at the button ui for how to do your yes and no options.

Then look at the animator for opening your door.

1 Like

That’s a lot of stuff. Please break it into parts small enough to tackle one by one.

Create your dialog box first, using Canvas, Image, Text, and Button. If you don’t know how to do this, then you need to work through some of the UI tutorials.

Then, create a script that watches for an “E” keypress using Input.GetKeyDown. The sample code given in the reference should be enough to get you started.

Then add a UnityEvent to your script, which you invoke when the key is pressed. Something like this:

using UnityEngine;
using UnityEngine.Events;

class TestScript : MonoBehaviour {
    UnityEvent onEPressed;

    void Update() {
        if (Input.GetKeyDown(KeyCode.E)) onEPressed.Invoke();
    }
}

Now you can start your dialog box inactive (uncheck the checkbox at the top of the inspector), and then use the above script (placed on an empty GameObject in the scene, or on the player, etc.) to set the dialog box active when E is pressed.

I have no idea what you mean by “I need it to have a 45% chance of saying yes and 55% of saying no,” so I’m going to ignore that. :slight_smile:

Now you have buttons on this dialog; making them play sounds doesn’t require any code at all (just throw an AudioSource on each one, and hook up the button click event to play it). Similarly, you can have the buttons deactivate the dialog, again via their onClick event, in exactly the same way that you had your E script activate it.

So now all that’s left is making those buttons actually do something. Add some public methods to your script — maybe call them YesClicked and NoClicked. And then simply invoke these methods from your button events.

If any of this is Greek to you, it means you are trying to squeak by without doing the UI tutorials. Go do them… none of this is very hard once you see how it’s done!

1 Like