• 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 /
avatar image
0
Question by Sad_Man30 · Aug 28, 2020 at 02:00 PM · 2d gametopdown

how to change the trigger?

i want to trigger only the subject when im in front of italt text

in this photo i can access the trash can anywhere as long as i click it this is the script of the trashcan

 public class Clickable : MonoBehaviour
 {
     public Dialogue dialogue;
     void OnMouseDown ()
     {
         FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
     }
 }
help2.png (269.0 kB)
Comment
Add comment · Show 1
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 Llama_w_2Ls · Aug 28, 2020 at 02:02 PM 0
Share

You can have a bool that is true onTriggerEnter with the trashcan's trigger, and the bool is false when OnTriggerExit. Then the function On$$anonymous$$ouseDown() can only occur if the bool is true.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ahsen35813 · Aug 28, 2020 at 02:43 PM

First, add a trigger around the trash can, and give the player the tag "Player".

Then, put your script FindObjectOfType<DialogueManager>().StartDialogue(dialogue); inside an if statement like so:

 if (triggered)
 {
     FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
 }

and also add these to your script:

 void OnTriggerEnter()
 {
    if (other.gameObject.CompareTag("Player"){triggered = true;}
 }
 void OnTriggerExit()
 {
    if (other.gameObject.CompareTag("Player"){triggered = false;}
 }

and this before any other code

 public bool triggered;

Now your script should be able to tell when the player is touching the trashcan, and only allow the dialogue to work when the player is touching the trashcan trigger. I hope this helps!

Comment
Add comment · Show 3 · 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 Sad_Man30 · Aug 29, 2020 at 05:07 AM 0
Share
 public class Clickable : $$anonymous$$onoBehaviour
 {
     public bool triggered;
     public Dialogue dialogue;
 
     void On$$anonymous$$ouseDown()
     {
         if (triggered)
         {
             FindObjectOfType<Dialogue$$anonymous$$anager>().StartDialogue(dialogue);
         }
     }
 
     void OnTriggerEnter()
     {
         if (other.gameObject.CompareTag("Player")
         {
             triggered = true;
         }
     }
     void OnTriggerExit()
     {
         if (other.gameObject.CompareTag("Player")
         { 
             triggered = false; 
         }
     }
 }

it's saying error CS1026:) expected in the line with the triggered = true/false; cant seem to find the error

avatar image Sad_Man30 Sad_Man30 · Aug 29, 2020 at 05:31 AM 0
Share

i found the problem thanks it work just forgot the ) hahaha

avatar image ahsen35813 Sad_Man30 · Aug 30, 2020 at 08:54 PM 0
Share

Oh yeah, I see where the error is. Sorry about that : /. I'm glad it worked in the end!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

174 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 avatar image avatar image

Related Questions

2D Rotation for aiming problem 1 Answer

2d top-down rpg direction check 1 Answer

How to get an enemy to go to a location and then return. 1 Answer

Prefab to a tile script 0 Answers

2D character Animation-controller 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