Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 02, 2018 at 06:27 PM by unity_qYQW964U5-CBQw for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by unity_qYQW964U5-CBQw · Jul 02, 2018 at 05:10 PM · uiinputtriggerui image

Showing UI image when inside a trigger and then pressing a button

So i want my player to move up to a sign on a wall with a trigger, which will then show the text "E to interact", that bit I worked out but i want an UI image to show up when my player is inside the trigger and presses E but i can't seem to detect the trigger and input at the same time.

 [SerializeField] private Image instructImg;
 
     private bool isTouching;
 
     private void OnTriggerStay2D(Collider2D other)
     {
         if (Input.GetKeyDown("e"))
         {
             isTouching = true;
         }    
         
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {      
         isTouching = false;
         
     }
 
     private void Update()
     {
         if (isTouching)
         {
             instructImg.enabled = true;
         }
         else
         {
             instructImg.enabled = false;
         }
     }

This is all i got right now, I have tried a some other approaches but none of them seem to work. Would really appreciate some help with this, thank you.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nabajit_ · Jul 02, 2018 at 05:22 PM 0
Share

Use Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)

avatar image unity_qYQW964U5-CBQw nabajit_ · Jul 02, 2018 at 05:27 PM 0
Share

Sorry but that didn't work, however after a few epithanies in a row I made it work so that the image only appears when my player is in the trigger and presses e and when he goes too far away or presses e again it just turns off again. With this code.

 [SerializeField] private Image instructImg;
 
     private bool isTouching;
 
     private void OnTriggerStay2D(Collider2D other)
     {
        
         isTouching = true;
          
         
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {      
         isTouching = false;
         
     }
 
     private void Update()
     {
         if (isTouching && Input.Get$$anonymous$$eyDown("e"))
         {
             instructImg.enabled = !instructImg.enabled;
         }
         else if (!isTouching)
         {
             instructImg.enabled = false;
         }
     }

2 Replies

  • Sort: 
avatar image
0

Answer by Priyanka-Rajwanshi · Jul 02, 2018 at 05:29 PM

@unity_qYQW964U5-CBQw Try this:

   private void OnTriggerEnter2D(Collider2D other)
  {
          isTouching = true;
  }
 
  private void OnTriggerExit2D(Collider2D other)
  {      
      isTouching = false;
  }
 
  private void Update()
  {
      if (isTouching && (Input.GetKeyDown(KeyCode.E)))
      {
          instructImg.enabled = true;
      }
      else
      {
          instructImg.enabled = false;
      }
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_qYQW964U5-CBQw · Jul 02, 2018 at 05:33 PM 0
Share

Thanks but I actually managed to figure it out like 2 $$anonymous$$utes ago.

avatar image Priyanka-Rajwanshi unity_qYQW964U5-CBQw · Jul 03, 2018 at 03:30 AM 0
Share

Please accept the answer if it's the solution.

avatar image
0

Answer by Epicepicness · Jul 02, 2018 at 05:57 PM

Is there a collider and a Rigidbody on the triggering object? I believe you need both of those in order for OnTriggerEnter and such to trigger properly. The following script worked for me:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ShowImage : MonoBehaviour {
     [SerializeField] private Image instructImg;
 
     private void OnTriggerStay2D () {
         if (Input.GetKeyDown ("e")) {
             ToggleImage (true);
         }
     }
 
     private void OnTriggerExit2D () {
         ToggleImage (false);
     }
 
     private void ToggleImage (bool enable) {
         if (instructImg.enabled != enable) {
             instructImg.enabled = enable;
         }
     }
 }

Practically the same thing, just without constant update checks.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

unity 2017 UI Delay bug 0 Answers

Script Repositions UI Element in the Wrong Spot 0 Answers

Shoot accuracy bar 1 Answer

Joystick not capturing clicks and drags 1 Answer

How to stretch top and bottom portions of a sprite without stretching the middle...,Stretching the top and bottom portions of a sprite without stretching the middle... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges