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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Activate Object on Trigger

Discussion in 'Scripting' started by TomsTales, Dec 17, 2020.

  1. TomsTales

    TomsTales

    Joined:
    Nov 3, 2020
    Posts:
    91
    Hi Guys, getting crazy. I know it is probably a very basic questions but heck I can't figure it out.

    I want to have an invisble Object (no problem) with a Collider set to trigger, and if the Player runs into this trigger, a box appears, doing nothing (not a UI box, just a Sprite). If the player leaves the collider, so goes the box.

    I know in theory what to do but it.is.not.working. Now I got somethign from the interweb that also looks good, but my setups still doesnt' work:

    Player Sprite: Has Rigidbody2D and various Colliders, one set as "Trigger", is tagged as "player"
    Box that should pop up: Has both, too
    Trigger, also has both ofc

    BOX has the following Script with "Trigger" set as the...well, trigger. Tried other things too, but meh.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ActiveObjects : MonoBehaviour
    6. {
    7.  
    8.     public GameObject trigger;
    9.  
    10.     void Start()
    11.     {
    12.         trigger.SetActive(false);
    13.     }
    14.  
    15.     void OnTriggerEnter2D(UnityEngine.Collider2D collision)
    16.     {
    17.         if (collision.gameObject.CompareTag("player"))
    18.         {
    19.             trigger.SetActive(true);
    20.         }
    21.     }
    22.  
    23.  
    24. }
    25.  
    I am happy to use ANYTHING else, whatever works, it mus be dead simple I know it...

    Cheers
     
    Last edited: Dec 17, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Note that objects that have been set to inactive cannot collide with things. (This may or may not be your problem, not enough info to tell.)

    A request for debugging help should explain what happens with your current code, and how that is different from what you wanted. If you're not sure what happens with your current code, you should add some Debug.Log statements to verify what code is actually running at what times.
     
  3. TomsTales

    TomsTales

    Joined:
    Nov 3, 2020
    Posts:
    91
    Hi Antistone, what info would you need for a better insight?

    If you have anything to point me at, happy to throw all away and use it...I mean it shouldnt be that hard to walk at a collider and open a picture, btu I cant find anything working only a lot of questions
     
  4. Havyx

    Havyx

    Joined:
    Oct 20, 2020
    Posts:
    140
    I'm confused as to why your player has a collider that is marked as a trigger.

    I'm also a little confused as to why you are using the namespace when you already declare "using UnityEngine".

    Next... if I understand correctly you have a box with a trigger

    so the box has this script.... but you're setting "trigger" as false. Although, this doesn't matter because if you have the box disabled then none if the code will be called anyway.

    I don't really do 2D so hopefully there isn't that much different.

    1) Create a box/plane ( a 2d plane?) and remove the mesh renderer but keep the collider (I assume it comes with a box collider). call it ExampleTrigger

    2) Mark that as "is trigger"

    3) Create another box (2d plane?) - this will be the box that is enabled/disabled.

    4) Add this script to the ExampleTrigger gameobject

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     public GameObject box;
    8.  
    9.     void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         if (other.gameObject.CompareTag("Player"))
    12.         {
    13.             box.SetActive(true);
    14.         }
    15.     }
    16.  
    17.     void OnTriggerExit2D(Collider2D other)
    18.     {
    19.         if (other.gameObject.CompareTag("Player"))
    20.         {
    21.             box.SetActive(false);
    22.         }
    23.     }
    24.  
    25. }
    26.  
     
  5. TomsTales

    TomsTales

    Joined:
    Nov 3, 2020
    Posts:
    91
    Hi Havyx, thanks! This works perfect (only thing that had to change was Collision2d to Collider2D) :)

    This starts the object visible of course, however I can just set it to unchecked in the inspector and it works!
     
  6. euriahmoore4

    euriahmoore4

    Joined:
    Sep 7, 2020
    Posts:
    1
    is it possible to modify it for unity 3d