0
\$\begingroup\$

I am trying to make a sign that brings up a panel when you press Enter while the player is over the sign. The code I have written is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SignScript : MonoBehaviour
{
    public GameObject Panel;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            UnityEngine.Debug.Log("Triggered1");
            if (Input.GetAxis("Submit") == 1)
            {
                UnityEngine.Debug.Log("Triggered2");
                if (Panel == false)
                {
                    Panel.SetActive(true);
                    UnityEngine.Debug.Log("Triggered3");
                }
            }
        }
    }
}

I added code that prints "Triggered1", "Triggered2", and "Triggered3" to the Debug Log when you reach certain parts of the code. When the player is over the sign and is pressing Enter, it should print "Triggered2". For some reason, it is not doing this. Does anyone know why?

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Here's a neater way, without the bug.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SignScript : MonoBehaviour
{
    public GameObject panel;

    // Start this script in the disabled state automatically.
    void Reset() {
        enabled = false;
    }

    // Collision events fire even if the script is disabled, 
    // so we can use them to wake it up:
    void OnTriggerEnter2D(Collider2D other)
    {
        // Consider using a physics layer that only contains the player,
        // and put this sign on a layer that only collides with the player layer,
        // so you don't even need to check tags: no overlap detection even happens for other stuff.
        if (other.CompareTag("Player"))
            enabled = true; // Start listening.
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
            enabled = false; // Stop listening.
    }        

    // Runs only when this script is enabled (ie. when a player is in the trigger).
    void Update() {
        // Use a button input for actions like this, not an axis.
        if (Input.GetButtonDown("Submit"))
            panel.SetActive(true);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, this works great. That said, I would add three corrections. First, the if statement in the Update method has no curly brackets. You also assign the panel to the GameObject variable "panel", while the rest of the code references "Panel". Finally, I also added a bool named enterPressed that is referenced in my panel script and set it to true in the if statement in the Update function. Other than those things, this works much better than my code. \$\endgroup\$
    – DRESphAl
    Commented Jun 6, 2020 at 16:23
0
\$\begingroup\$

I found a solution. It causes one bug that I know of, but it works for right now. The code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SignScript : MonoBehaviour
{
    public GameObject Panel;
    public bool pressingEnter;
    public bool panelNotActive;
    public bool enterPressed;

    void Update()
    {
        if (Input.GetAxis("Submit") == 1)
        {
            pressingEnter = true;
        }

        if(Input.GetAxis("Submit") == 1)
        {
            if(panelNotActive)
            {
                Panel.SetActive(true);
                UnityEngine.Debug.Log("Triggered3");
                enterPressed = true;
                panelNotActive = false;
                pressingEnter = false;
                Destroy(gameObject);
            }
        }

        pressingEnter = false;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            UnityEngine.Debug.Log("Triggered1");
            if (Panel.activeSelf == false)
            {
                UnityEngine.Debug.Log("Triggered2");
                panelNotActive = true;
            }
        }
    }
}

In Unity, I put the panel I want to bring up on the "Panel" variable. I also made a second script that goes on the panel I am bringing up. The code of that is:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;

public class PanelScript : MonoBehaviour
{
    public GameObject sign;
    private SignScript signScript;

    // Start is called before the first frame update
    void Start()
    {
        signScript = sign.GetComponent<SignScript>();
    }

    // Update is called once per frame
    void Update()
    {
        if(signScript.enterPressed)
        {
            if (Input.anyKey)
            {
                if(Input.GetAxis("Submit") != 1)
                {
                    gameObject.SetActive(false);
                }

            }
        }
        else if (Input.anyKey)
        {
            gameObject.SetActive(false);
        }
    }
}

As I mentioned earlier, this has one bug I know of. The bug is, if you walk past the sign without pressing Enter, the next time you press enter it will bring up the panel. Even though this is okay for now, if anyone knows how to remove the bug or make the code better, feel free to answer. Thanks!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .