- Home /
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?
Answer by JackhammerGaming · Jun 07, 2021 at 06:41 PM
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.
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.
Thanks for the quick response! If anyone else uses anything else other than triggers please let me know!