Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by MidievalMan · Jun 07, 2021 at 06:28 PM · triggersefficiencybestpracticesbest practicesbest-practices

How to Make Things Happen the Right Way

Background: 1.5 year (and relatively young) programmer who knows basic coding, but doesn’t know how to use medium-advanced structures like structs, events, and dictionaries. Also pretty new to Unity, having used it some last summer and so far this summer too.

Problem: I wanted my 2D platformer game to be more interactive, so I wanted things to happen when I did certain tasks or went to certain places (blockades to disappear, coins to appear, new dialogues to show after completing a quest)

Current solution: Right now I’m using 2 strategies to work through this problem. However, both of them feel a little bit unclean and there is some duplicate code.

Solution 1: The first solution is to use an empty gameobject with a box collider set to “is trigger” and script attached that detect when the player gets nearby (using OnTriggerEnter2D and an if statement that checks the tag of the collision). If it’s triggered by the player, I simply do something inside. For example, I’ve made a prefab that plays a sound when the player steps into the box.

Solution 2: Add to scripts two public arrays of GameObjects that when something happens in the code (triggering dialogue, pressing a button, entering a box collider trigger) will go through the first array and make all the objects SetActive(true) and go through the second array and make all the objects SetActive(false). This works alright, but it forces me to copy and paste the same code into several different scripts (a dialogue script, a button script, and a trigger script for example).

Conclusion/Question: All in all, I just feel like I’m doing it wrong and with too much dragging and dropping in the inspector and too much duplication in my coding. How do people usually do this type of thing/does anyone have any tips for how to get this done better or more efficiently?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JackhammerGaming · Jun 07, 2021 at 06:41 PM

@MidievalMan

  1. For triggering actions i always use colliders with "IsTrigger = true" i almost see evreyone do the same thing there might be other ways but they are definitely not as efficient and simple as the simple trigger check.

  2. I today only made a dialogue system for game i just needed one script with an array of all dialogues placed inside a function to iterate with a single button click it was pretty easy and didn't take time.

Comment
Add comment · Show 1 · 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 MidievalMan · Jun 07, 2021 at 07:09 PM 0
Share

Thanks for the quick response! If anyone else uses anything else other than triggers please let me know!

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.

Follow this Question

Answers Answers and Comments

121 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

Related Questions

Should player & pickups be children of the level, or keep them separate? 2 Answers

[UNet] Best practice to cache components in NetworkBehaviour 0 Answers

Best Saving Practices using Firebase 0 Answers

Best Practices 1 Answer

Best way to check object selection 0 Answers


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