Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

problem with trigger zone

Discussion in 'Scripting' started by Trild123787898, Feb 17, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    when I touch, I remove the components; if I add, I add, the problem is that when I enter the component trigger I delete, but when I go in they quickly created and are seen again, about 1 second, how can I solve this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Swipe : MonoBehaviour
    8. {
    9.  
    10.  
    11.     public GameObject det1;
    12.     public ConfigurableJoint joint;
    13.     public Rigidbody rb;
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         joint = det1.GetComponent<ConfigurableJoint>();
    23.     }
    24.  
    25.     void OnTriggerEnter(Collider enter)
    26.     {
    27.         if (enter.tag == "terrain")
    28.         {
    29.             Debug.Log("Enter");
    30.             Destroy(det1.GetComponent<ConfigurableJoint>());
    31.             Destroy(det1.GetComponent<Rigidbody>());
    32.         }
    33.     }
    34.     void OnTriggerExit(Collider exit)
    35.     {
    36.         if (exit.tag == "terrain")
    37.         {
    38.             Debug.Log("Exit");
    39.             det1.AddComponent<Rigidbody>();
    40.             joint = det1.AddComponent<ConfigurableJoint>();
    41.        
    42.             joint.connectedBody = rb;
    43.  
    44.  
    45.             //det1.AddComponent<Rigidbody>();
    46.         }
    47.     }
     
  2. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    Instead of Destroying and Adding the Components, you can just enable or disable them

    EDIT : Sorry My Bad. You can't disable or Enable these components
     
    Last edited: Feb 17, 2020
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    this is not the component that can be switched on or off
     
  4. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    Ah, Sorry, I should have checked.

    Can you make sure that OnTriggerExit and OnTriggerEnter are only being called once? Or you don't have any other script which is adding these components?
     
  5. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry already decided, scored the axis on locked put