One Button Multiple Collision OnTrigger

I’m trying to make a single button handles each code in one script. For example, as you can see below there is red and blue. In red, you have a task to let’s say wash the dishes and in blue, you have to wash the clothes. I know I could have saved a lot of time by putting different scripts in each GameObject that handles different codes but I wanted to try making something new by doing these tasks in just a single script well 2 scripts one for the button and one for the collision. I was planning on using arrays but it seems complicated to me. So I’m asking you guys to help me out. Any help is appreciated.

So in this scene, I have two cubes with “Is Trigger” on.
So the cube that is colored Red is “Task 1” and Blue is “Task 2”
6429026--719045--TriggerCollisions.JPG

So in each cube, I added a single script that handles each task.
6429026--719057--Script.JPG
6429026--719051--task2.JPG

Code for the DetectTriggerEvent

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class DetectTriggerEvent : MonoBehaviour
{
    public GameObject InteractBttn;
    public Button UseBttn;
    public string Task;
    public bool Use;

    private void Start()
    {
        UseBttn = UseBttn.GetComponent<Button>();
    }

    private void OnTriggerEnter(Collider other)
    {
        // InteractBttn.SetActive(true);
        UseBttn.interactable = true;
        UseBttn.image.color = new Color(0 / 255f, 255 / 255f, 90 / 255f);
        Use = true;
    }

    private void OnTriggerExit(Collider other)
    {
        // InteractBttn.SetActive(false);

        UseBttn.interactable = false;
        UseBttn.image.color = new Color(38 / 255f, 38 / 255f, 38 / 255f);
        Use = false;
    }
}

Code for OnClickEvent

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnClickEvent : MonoBehaviour
{
    public GameObject UIInteraction;
    public string CurrentTask;
    public bool Use;

    private void Update()
    {
        DetectTriggerEvent UseBoolScript = UIInteraction.GetComponent<DetectTriggerEvent>();
        CurrentTask = UseBoolScript.Task;
        Use = UseBoolScript.Use;

        if (Input.GetKeyDown(KeyCode.E)) { OnClick(CurrentTask); }
    }

    public void OnClick(string ctask)
    {

        if (Use == true)
        {
            switch(ctask)
            {
                case "Task 1":
                    Debug.Log("Task 1");
                    break;

                case "Task 2":
                    Debug.Log("Task 2");
                    break;
            }
        }
    }
}

Task 1 seems to work but Task 2 doesn’t. Any ideas?

6429026--719045--TriggerCollisions.JPG
6429026--719057--Script.JPG

Conceptually, break it apart into stages.

  1. When a button is pressed, have the code only note “There has been a user button intent.”

  2. Now have a separate block of code that decides “What does that intent mean?”

  3. Finally, execute the appropriate thing based on the decision in #2 above.

Alright, Thank you for help me out I did what you did and I changed some stuff. Although I encountered a problem please take a look: https://gyazo.com/721c9fd9bd74666d41017ab2ee851471

Task2 have the same properties as Task1 but Task2 wont work. Sorry if my English is not good, I’m also somewhat new to Unity I hope you understand :^)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnClickEvent : MonoBehaviour
{
    public GameObject Player;
    public GameObject UIInteraction;
    public string CurrentTask;
    public bool Use;

    private void Update()
    {
        DetectTriggerEvent UseBoolScript = UIInteraction.GetComponent<DetectTriggerEvent>();
        PlayerMovement pl = Player.GetComponent<PlayerMovement>();
        CurrentTask = pl.CurrentTask;
        Use = UseBoolScript.Use;
        if (Input.GetKeyDown(KeyCode.E)) {  OnClick(CurrentTask); }
    }

    public void OnClick(string ctask)
    {
      
        if (Use == true)
        {
            switch(ctask)
            {
                case "Task1":
                    Debug.Log(ctask);
                    break;

                case "Task2":
                    Debug.Log(ctask);
                    break;
            }
        }
        CurrentTask = "";
    }
}

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 are the values of the variables involved? Are they initialized?

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