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

Question enable and disable component through a coroutine in an if statement (beginner)

Discussion in 'Scripting' started by tannenbraum, Jul 16, 2023.

  1. tannenbraum

    tannenbraum

    Joined:
    Jul 11, 2023
    Posts:
    5
    Good day,
    i would like to receive help regarding the following code because my boxcollider does active but not deactive with the coroutine.
    Code (CSharp):
    1.         ...
    2.             BoxCollider m_boxcollider = nearbyObject.GetComponent<BoxCollider>();
    3.             if (m_boxcollider != null)
    4.             {
    5.              
    6.                 StartCoroutine(waitstuntimeboxcollider(m_boxcollider));
    7.  
    8.             }
    9.  
    10.  
    11.         }
    12.  
    13.         IEnumerator waitstuntimeboxcollider(BoxCollider boxcolliderplaceholder)
    14.         {
    15.             boxcolliderplaceholder.enabled = true;
    16.             yield return new WaitForSeconds(2);
    17.             boxcolliderplaceholder.enabled = false;
    18.         }
    Thanks for reading!
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    It works fine. What are you expecting to happen when the box collider is disabled?. Do you want the box to vanish?.
     
    tannenbraum likes this.
  3. tannenbraum

    tannenbraum

    Joined:
    Jul 11, 2023
    Posts:
    5
    I want to displace the GreenSymbol Character Controller during the flight through an explosion with a box collider.
    Maybe my Unity Version is the problem because the Box Collider really does not deactivate after 2 seconds: upload_2023-7-16_12-40-0.png upload_2023-7-16_12-40-16.png
    I do think i do the same as others and it does not work in my case somehow^^
     

    Attached Files:

  4. tannenbraum

    tannenbraum

    Joined:
    Jul 11, 2023
    Posts:
    5
    Wahrscheinlich besser wenn ich das ganze Skript poste: Grenade for FPS Micro Game(noch nicht fertig):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.FPS.Game;
    4. using Unity.FPS.Gameplay;
    5. using Unity.FPS.UI;
    6. using UnityEngine;
    7.  
    8. public class Grenade : MonoBehaviour {
    9.  
    10.     public float delay = 3f;
    11.     public float radius = 5f;
    12.     public float force = 700f;
    13.     public GameObject explosionEffect;
    14.  
    15.     float countdown;
    16.  
    17.     bool hasExploded = false;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         countdown = delay;  
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         countdown -= Time.deltaTime;
    28.         if(countdown <= 0 && !hasExploded){
    29.             Explode();
    30.             hasExploded = true;
    31.         }
    32.     }
    33.  
    34.  
    35.     public int coroutinestartstop = 1;
    36.  
    37.     void Explode(){
    38.         //Show Effect
    39.        
    40.         Instantiate(explosionEffect,transform.position,transform.rotation);
    41.  
    42.         //Get nearby objects
    43.         Collider[] colliderstodestroy = Physics.OverlapSphere(transform.position,radius);
    44.        
    45.  
    46.         foreach (Collider nearbyObject in colliderstodestroy)
    47.         {
    48.  
    49.  
    50.             Destruction2 m_destruction2 = nearbyObject.GetComponent<Destruction2>();
    51.             //Apply force
    52.             if (m_destruction2 != null)
    53.             {
    54.                 m_destruction2.Destroyer();
    55.             }
    56.             Health m_health2= nearbyObject.GetComponent<Health>();
    57.             if(m_health2 != null)
    58.             {
    59.                 m_health2.TakeDamage(23, gameObject);
    60.             }
    61.         }
    62.  
    63.         Collider[] colliderstomove = Physics.OverlapSphere(transform.position, radius);
    64.         foreach (Collider nearbyObject in colliderstomove) {
    65.             CharacterController m_charactercontroller = nearbyObject.GetComponent<CharacterController>();
    66.             if (m_charactercontroller != null)
    67.             {
    68.                 m_charactercontroller.enabled = false;
    69.             }
    70.            
    71.             Rigidbody rb = nearbyObject.GetComponent<Rigidbody> ();
    72.             //Apply force
    73.             if (rb != null) {
    74.                 rb.isKinematic = false;
    75.                 rb.AddExplosionForce (force, transform.position, radius);
    76.             }
    77.  
    78.             if (m_charactercontroller != null)
    79.             {
    80.                 m_charactercontroller.enabled = false;
    81.             }
    82.             BoxCollider m_boxcollider = nearbyObject.GetComponent<BoxCollider>();
    83.             if (m_boxcollider != null)
    84.             {
    85.                
    86.                 StartCoroutine(waitstuntimeboxcollider(m_boxcollider));
    87.  
    88.             }
    89.  
    90.  
    91.         }
    92.  
    93.  
    94.  
    95.         //Remove Grenade
    96.         Destroy(gameObject);
    97.     }
    98.     IEnumerator waitstuntimeboxcollider(BoxCollider boxcolliderplaceholder)
    99.     {
    100.         boxcolliderplaceholder.enabled = true;
    101.         yield return new WaitForSeconds(2);
    102.         boxcolliderplaceholder.enabled = false;
    103.     }
    104.  
    105. }
    106.  
     
  5. tannenbraum

    tannenbraum

    Joined:
    Jul 11, 2023
    Posts:
    5
    I did notice that the IEnumerator was placed in the Exlode function before but putting it outside did not solve the problem.
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,111
    Well, you do DESTROY your gameObject immediately after starting the coroutine. Coroutines are bound to the MonoBehaviour that started them. So if you destroy the object your coroutine will also be gone.

    Basically, you tell your grenade to do something in 2 seconds. But then you kill it immediately. Two seconds later that poor grenade is not around anymore to do your bidding. It's dead, gone.

    D1FEA33A-DF43-4F71-AF75-BB828715827B.gif
     
    Last edited: Jul 16, 2023
    tannenbraum likes this.
  7. tannenbraum

    tannenbraum

    Joined:
    Jul 11, 2023
    Posts:
    5
    That makes sense thanks xD And thx zulo3d too! Will post full codes in the future!