Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question One Button Multiple Collision OnTrigger

Discussion in 'Scripting' started by Dichill, Oct 18, 2020.

  1. Dichill

    Dichill

    Joined:
    Apr 9, 2019
    Posts:
    3
    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"
    TriggerCollisions.JPG

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

    Code for the DetectTriggerEvent
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DetectTriggerEvent : MonoBehaviour
    7. {
    8.     public GameObject InteractBttn;
    9.     public Button UseBttn;
    10.     public string Task;
    11.     public bool Use;
    12.  
    13.     private void Start()
    14.     {
    15.         UseBttn = UseBttn.GetComponent<Button>();
    16.     }
    17.  
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         // InteractBttn.SetActive(true);
    21.         UseBttn.interactable = true;
    22.         UseBttn.image.color = new Color(0 / 255f, 255 / 255f, 90 / 255f);
    23.         Use = true;
    24.     }
    25.  
    26.     private void OnTriggerExit(Collider other)
    27.     {
    28.         // InteractBttn.SetActive(false);
    29.  
    30.         UseBttn.interactable = false;
    31.         UseBttn.image.color = new Color(38 / 255f, 38 / 255f, 38 / 255f);
    32.         Use = false;
    33.     }
    34. }
    35.  
    Code for OnClickEvent
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class OnClickEvent : MonoBehaviour
    7. {
    8.     public GameObject UIInteraction;
    9.     public string CurrentTask;
    10.     public bool Use;
    11.  
    12.     private void Update()
    13.     {
    14.         DetectTriggerEvent UseBoolScript = UIInteraction.GetComponent<DetectTriggerEvent>();
    15.         CurrentTask = UseBoolScript.Task;
    16.         Use = UseBoolScript.Use;
    17.  
    18.         if (Input.GetKeyDown(KeyCode.E)) { OnClick(CurrentTask); }
    19.     }
    20.  
    21.     public void OnClick(string ctask)
    22.     {
    23.  
    24.         if (Use == true)
    25.         {
    26.             switch(ctask)
    27.             {
    28.                 case "Task 1":
    29.                     Debug.Log("Task 1");
    30.                     break;
    31.  
    32.                 case "Task 2":
    33.                     Debug.Log("Task 2");
    34.                     break;
    35.             }
    36.         }
    37.     }
    38. }
    39.  
    Task 1 seems to work but Task 2 doesn't. Any ideas?
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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.
     
  3. Dichill

    Dichill

    Joined:
    Apr 9, 2019
    Posts:
    3
    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 :^)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class OnClickEvent : MonoBehaviour
    7. {
    8.     public GameObject Player;
    9.     public GameObject UIInteraction;
    10.     public string CurrentTask;
    11.     public bool Use;
    12.  
    13.     private void Update()
    14.     {
    15.         DetectTriggerEvent UseBoolScript = UIInteraction.GetComponent<DetectTriggerEvent>();
    16.         PlayerMovement pl = Player.GetComponent<PlayerMovement>();
    17.         CurrentTask = pl.CurrentTask;
    18.         Use = UseBoolScript.Use;
    19.         if (Input.GetKeyDown(KeyCode.E)) {  OnClick(CurrentTask); }
    20.     }
    21.  
    22.     public void OnClick(string ctask)
    23.     {
    24.      
    25.         if (Use == true)
    26.         {
    27.             switch(ctask)
    28.             {
    29.                 case "Task1":
    30.                     Debug.Log(ctask);
    31.                     break;
    32.  
    33.                 case "Task2":
    34.                     Debug.Log(ctask);
    35.                     break;
    36.             }
    37.         }
    38.         CurrentTask = "";
    39.     }
    40. }
    41.  
     
    Last edited: Oct 19, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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.