Search Unity

Menu to open on collsion

Discussion in 'Scripting' started by ryanlaggar, Jul 15, 2022.

  1. ryanlaggar

    ryanlaggar

    Joined:
    Jul 5, 2022
    Posts:
    9
    Hi there,

    I am wanting to pause menu to pop open when i collide with the sprite tagged "Mother."
    I have tested and the pause menu opens with a button however I am unsure what code to write to get it to open when you collide with something.

    The script containing the "// what script goes here" is what I implemented and tried.

    Almost like a a congratulations page pops up when the character reaches the end of a level and you get the options to restart or go to the main menu..

    Please let me know, or even if the question is hard to understand. Thanks in advance!



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] GameObject pauseMenu;
    10.  
    11. public void Pause()
    12.     {
    13.         pauseMenu.SetActive(true);
    14.         Time.timeScale = 0.5f;
    15.     }
    16.  
    17.     public void Resume()
    18.     {
    19.         pauseMenu.SetActive(false);
    20.         Time.timeScale = 1f;
    21.     }
    22.     public void Home(int sceneID)
    23.     {
    24.         Time.timeScale = 1f;
    25.         SceneManager.LoadScene(sceneID);
    26.     }
    27.  
    28.     private void OnTriggerEnter2D(Collider2D collision)
    29.     {
    30.         if (collision.tag == "Mother")
    31.         {
    32.             //what script goes here
    33.         }
    34.     }
    35. }
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    Unless I misunderstood the question, you wrote your own answer: write the same code that's called when pressing the button.
     
    ryanlaggar likes this.
  3. ryanlaggar

    ryanlaggar

    Joined:
    Jul 5, 2022
    Posts:
    9
    Thanks you made me have a second look, I've been working too long and missing simple mistakes