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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

enable object when other is disabled and so on

Discussion in 'Scripting' started by ch66666, Jul 3, 2020.

  1. ch66666

    ch66666

    Joined:
    Jul 3, 2020
    Posts:
    7
    how would a script look like that allows me to disable an item once i enter the trigger of it, and when i enter the trigger, another item enables itself and so on? example; cube1 disables and cube2 enables, cube2 disables and cube3 enables, cube3 disables and cube4 enables, and so
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    For this to work you have to assign Cube2 to the slot ConnectedGameObject and attach this script to Cube1.
    Cube1 must have a trigger collider. The player should have the tag "Player".

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ConnectedGameObjects : MonoBehaviour
    7. {
    8.     public GameObject ConnectedGameObject;
    9.  
    10.     //When the player triggers this GO it will disable itself and active the connected GameObject
    11.     private void OnTriggerEnter(Collider other)
    12.     {
    13.         //Check to see if the tag on the collider is equal to Player
    14.         if (other.tag == "Player")
    15.         {
    16.             Debug.Log("Triggered by Player");
    17.             // Deactivate myself
    18.             this.gameObject.SetActive(false);
    19.  
    20.             // Activate my connected GameObject
    21.             ConnectedGameObject.SetActive(true);
    22.         }
    23.     }
    24. }
    25.  
    26.  
     
    ch66666 likes this.
  3. ch66666

    ch66666

    Joined:
    Jul 3, 2020
    Posts:
    7
    thank you very much, means a lot :,) i thought just way too complicated about this and i kinda stuck myself in a bad position but you helped me easily. thanks again.
     
    Zer0Cool likes this.