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”
So in each cube, I added a single script that handles each task.
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?