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. Dismiss Notice

Resolved Activate/Deactivate Object With Script Not Working?

Discussion in 'Scripting' started by AdamEarlston, Feb 27, 2021.

  1. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Hey, so basically I'd like to ask why this script isn't working as intended (I intend for the object to be deactivated on start up, and then when i walk into the invisible box collider, the object is enabled, and when i leave the box collider, the object is disabled again (only enabled whilst being inside the box collider basically))?
    When i launch the game, the object is disabled, and when i walk inside of the box collider nothing happens (the event should be triggered which enables the object though).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Teleportation : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Portal;
    9.     public GameObject Player;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         GameObject.Find("Portal Effect").SetActive(false);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }
    23.  
    24.     public void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.         if (other.gameObject.tag == "Player")
    27.         {
    28.             GameObject.Find("Portal Effect").SetActive(true);
    29.  
    30.         }
    31.         else
    32.         {
    33.             GameObject.Find("Portal Effect").SetActive(false);
    34.         }
    35.     }
    36. }
    Also this is kind of my first time creating a script without following a youtube guide or just copying everything, trying to learn the basics now. (besides from the part below)
    So my 2nd question is what does this part mean o.o?
    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    ^This part was from the original teleport script which i don't understand. I understand the "public void" part only pretty much. Well i get what it does (if the player enters the Collider2D, trigger the event) but i don't understand what each word/part means exactly. (if that makes sense o.o)
     
  2. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    GameObject.Find() won't find an inactive object. You should keep a reference to your portal effect when you look for it in your start method. Then use that to activate/deactive it in your OnTriggerEnter method.
    Code (CSharp):
    1. public class Teleportation : MonoBehaviour
    2. {
    3.  
    4.     public GameObject Portal;
    5.     public GameObject Player;
    6.     private GameObject portalEffectGo;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         portalEffectGo = GameObject.Find("Portal Effect")
    12.         portalEffectGo.SetActive(false);
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.     public void OnTriggerEnter2D(Collider2D other)
    22.     {
    23.         if (other.gameObject.tag == "Player")
    24.         {
    25.             portalEffectGo.SetActive(true);
    26.  
    27.         }
    28.         else
    29.         {
    30.             portalEffectGo.SetActive(false);
    31.         }
    32.     }
    33. }
     
  3. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Thanks! Guess that explains why it wasn't working lol. Going to re-read this several times to take it in.
     
  4. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    For some reason it still doesn't work though, it's disabled at the start and then fails to do anything when the Player enters the box collider
     
  5. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Oh it's because i had the script on 2 objects, so the 2nd object was keeping it disabled. Hmm i'll think about the solution whilst i'm afk for 30 mins.
     
  6. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Glad you got it sorted! Keep learning. It's great
     
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    It probably was activating and deactivating at once with two collisions/triggers. The script should only be on the portal. Also you don't need the reference to the Player unless you need to do something to it in script.

    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    OnTriggerEnter2D is called by Unity when the trigger happens, the Collider2D is the object type that is returned, and "other" (the collider2D that's returned) can be any variable name you want. You're then looking at the tag on that collider's gameObject (the collider is a component of the gameObject). So if it's tagged "Player", then "do something".

    Use GameObject.Find() sparingly, if at all, and don't use it more than once in a script for the same object, that's wasteful. Better to hold a public reference for anything you need to access and drag the object to the slot in the Inspector.
     
  8. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Thanks, i had another issue (maybe the same issue) where the portal would be deactivated twice when i entered the collider/trigger, at first i thought it was due to my player having several Player layers (but it requires a tag, not a layer i guess), and then i thought it was my weapon having a rigidbody + 2d collider as well >.< but ultimately it turned out to be that i didn't fully understand how OnTriggerEnter worked, i needed to set up a OnTriggerExit command to deactivate when i leave the area. Without it, it was just doing a deactivate first, then activate then deactivate all at once strangely (instead of activate and then deactive once i leave).

    But all of that is sorted now :D Learned some more stuff.
     
    seejayjames likes this.