pressing "E" to activate dialogue

Quick and easy question; what easy line of code can I use on lines 79 and 110 of the document below so the player would need to press “E” in a certain range of the npc rather than when the player enters a collider? thanks in advance!

3291692–254941–DialogueManager.cs (2.47 KB)

You should post your code within the forum within code tags. People generally don’t want to download your files to their computer to help with a simple code question.

Maybe something like on your tigger methods set and unset boolean value isInRange

    private bool isInRange;

    void OnTriggerEnter(Collider other) {

        if (other.gameObject.tag == "Tuto")
        {
            isInRange = true;
        }
    }

    void OnTriggerExit(Collider other) {

        if (other.gameObject.tag == "Tuto")
        {
            isInRange = false;
        }

    }

and on your Update method check for both pressed key and isInRange.

    private void Update()
    {
        if (isInRange && Input.GetKeyDown(KeyCode.E))
        {
            ShowDialog();
        }
    }

I will try this! It looks really promising thanks!

I’ll keep that in mind, thanks for the heads up!

My new code is:

private void Update1() {

if (isInRange && Input.GetKeyDown(KeyCode.E))
{

currentLine = 0;

dialogueActive = true;
talkingToTuto = true;
dialogueBox.SetActive (true);

//lock motion
GameObject varGameObject = GameObject.FindWithTag(“Player”);
varGameObject.GetComponent ().enabled = false;
}

I’m not sure if I did this correct bc it has no effect, must be because of my limited knowledge with C#. I prob could fix this myself if I knew how to script. Another point to note is that I first replaced void OnTriggerEnter(Collider other) { with private void Update1(){ and it said the (other.) of if (other.gameObject.tag == “Tuto”) { does not work unless I delete it

MonoBehaviour.Update() is Unity method which is called every frame, you shoud check player’s input in there. So if you will name the method Update1() it will not work.

When you add code, it’s best when you post it using code tags.

The methods you’ve renamed have a special name that’s used by the engine to call them. Just like Start, Update and many more.

The variable ‘other’ is no longer known/accessible in your renamed method, because you’ve not only changed the method’s name, but also removed the parameter list - which was ‘Collider other’ before, hence the name is not known.

I’d recommend to take a step back and work through some of the official tutorials first.

The problem with the Update() needing to be Update1() is because when I tried to copy what u had, there already was void Update() so they cant both be named the same. Unless I leave out the private void update and go straight to “if (isInRange && Input.GetKeyDown(KeyCode.E))” but that doesnt work either. I clearly have no knowledge of coding so if its not close to just copy and paste I seem to mess it up. Thanks for all your help though!

Thanks, that clears up alot! only problem is I do not have time to go through tutorials first because my deadlines are catching up, which led me to here. :confused:

So I put the:

[LIST=1]
[*]    private bool isInRange;
[*]

[*]    void OnTriggerEnter(Collider other) {
[*]

[*]        if (other.gameObject.tag == "Tuto")
[*]        {
[*]            isInRange = true;
[*]        }
[*]    }
[*]

[*]    void OnTriggerExit(Collider other) {
[*]

[*]        if (other.gameObject.tag == "Tuto")
[*]        {
[*]            isInRange = false;
[*]        }
[*]

[*]    }
[/LIST]

where my other bools are, above void start and update, but the actual code below is giving me trouble i dont understand.

    void OnTriggerEnter(Collider other) {

        if (isInRange && Input.GetKeyDown(KeyCode.E)) {

            currentLine = 0;

            dialogueActive = true;
            talkingToTuto = true;
            dialogueBox.SetActive (true);

            //lock motion
            GameObject varGameObject = GameObject.FindWithTag("Player");
            varGameObject.GetComponent<PlayerController> ().enabled = false;
        }

the problem here is that it says the script name, DialogueManager, already defines a member called onTriggerEnter. I realize this is because i have it both in the bool and in the void update. the problem is I do not know what to change it to bc deleting it caused more problems

The second code portion that you posted should be in Update(). :slight_smile:

You don’t have to replace the content of your Update or OnTriggerEnter methods, just append your new code after the existing code. But it must be inside these special methods for everything to work properly.

Yeah well you’ll just be having problems forever until you do. So after your deadlines, you should go through tutorials or you’ll keep asking forum and getting it wrong. There’s no shortcut for understanding it properly here.

That makes a lot more sense, I can’t believe I didn’t notice that!

I’ll keep that in mind next time

I’ll be playin around with that and looks like I’m on the right track! Thanks

Glad you’re on the right track :slight_smile: