How to detect if player is touching a specific GameObject

I Wanted to make it so that if the player touches a certain gameobject then time would stop and isPlayerAlive would be set to false. And it would stop spawning pipes I had this code set up

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

public class SpawnManager : MonoBehaviour
    public GameObject[] PipePrefabs;
    public int PipeIndex;
    public float SpawnInterval = 1;
    public float startDelay = 2;
    public bool isPlayerAlive = true;
    public GameObject DeathCanvas;
    // Start is called before the first frame update
    void Start()
        InvokeRepeating("SpawnRandomPipeType", startDelay, SpawnInterval);

    // Update is called once per frame
    void Update()
        if(collision.gameObject.tag == "Player")
            isPlayerAlive = false;
            Time.timeScale = 0f;

    void SpawnRandomPipeType()
        if(isPlayerAlive == true)
        int PipeIndex = Random.Range(0, 10);
        Instantiate(PipePrefabs[PipeIndex], new Vector3(20, 0, 0), PipePrefabs[PipeIndex].transform.rotation);  


You can use a OnTriggerEnter() function and check the tag of the object that touch the player and if the tag is the right one, then do the stuff you want inside.

1 Like

Thank you! This worked