Search Unity

Door Triggers open or close with button everywhere.

Discussion in 'Scripting' started by Anaktas24, Jun 18, 2021.

  1. Anaktas24

    Anaktas24

    Joined:
    Jun 22, 2020
    Posts:
    1
    Hello,

    Ive made a door and i watched a YT video for a siple TriggerEvent to open it( remove sprite) and close it (add sprite). The thing is, when i push the specific key it works just fine ,but the problem is it opens/closes even if the character isnt near the door.

    Thank you in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Then go back because you probably missed an important step in the tutorial.

    If you are convinced you have not missed a step, then find another tutorial.

    Otherwise, you can always try to diagnose the issue with your current code / setup:

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Also note that if you're asking for help with a script, you need to post that script along with your description of what is suppose to happen, what is actually happening, and any error codes if they occur. And use code tags!
     
    Kurt-Dekker likes this.
  4. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Right. So how are you going to check if the player is close to it?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Refer to post above. Redo the tutorial if you think you missed something, or find a good tutorial.

    REMEMBER: IT WILL NOT JUST BE A SCRIPT. The script portion is trivial will look something like:

    Code (csharp):
    1. if (NearEnoughDoor())
    2. {
    3.   if (OpenDoorCommanded())
    4.   {
    5.     OpenTheDoor();
    6.   }
    7. }
    But 100% of that is dependent on the scene setup and what the tutorial has caused you to do. If you don't understand this, go start with some door opening tutorials.