Search Unity

onTriggerExit error

Discussion in 'Scripting' started by freze00100, May 18, 2020.

  1. freze00100

    freze00100

    Joined:
    Jul 2, 2019
    Posts:
    9
    basically i have a script to stream terrains is verry simple
    Code (CSharp):
    1.  void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.tag == "Loader")
    4.         {
    5.             this.gameObject.transform.GetChild(0).gameObject.SetActive(true);
    6.         }
    7.     }
    8.     void OnTriggerExit(Collider col)
    9.     {
    10.         if (col.tag == "Loader")
    11.         {
    12.             this.gameObject.transform.GetChild(0).gameObject.SetActive(false);
    13.         }
    14.     }
    it worked normally but yesterday a random error just pop up:
    Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy instead.
    UnityEngine.GameObject:SetActive(Boolean)
    Streamer:OnTriggerExit(Collider) (at Assets/Scripts/Streamer.cs:18)

    the line 18 is :
    Code (CSharp):
    1. this.gameObject.transform.GetChild(0).gameObject.SetActive(false);
    can someone tell me why is this happening ?
     
  2. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Perhaps you have a DestroyImmediate call inside an OnDisable event in a script attached to some child? Just guessing...
     
  3. freze00100

    freze00100

    Joined:
    Jul 2, 2019
    Posts:
    9
    not really just the terrain from unity with no scripts attached no more gameobjects just the terrain.
     
  4. SamRock

    SamRock

    Joined:
    Sep 5, 2017
    Posts:
    250
    I am having the same problem. All I am doing is hiding Terrains, AI and objects that are faraway based on player's postion.


    Code (CSharp):
    1.  private void OnTriggerEnter(Collider other)
    2.     {
    3.         if ((DetectLayer & 1 << other.gameObject.layer) == 1 << other.gameObject.layer)
    4.         {
    5.             foreach (Transform sec in SectionsHide)
    6.             {
    7.                 sec.gameObject.SetActive(false);
    8.             }
    9.  
    10.             foreach (Transform sec in SectionsUnHide)
    11.             {
    12.                 sec.gameObject.SetActive(true);
    13.             }
    14.         }
    15.     }

    I keep getting this error:

    Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy instead.
    UnityEngine.GameObject:SetActive(Boolean)
     
    kasimridvan likes this.