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 Call a function from another script to detach child object

Discussion in 'Scripting' started by FriedPollo, Oct 22, 2020.

  1. FriedPollo

    FriedPollo

    Joined:
    Sep 24, 2020
    Posts:
    13
    Hi guys, I'm trying to create a "trojan horse" means there's a parent object and when it dies it releases other objects.
    screenshot: https://prnt.sc/v4qg5x

    this is the code where I store the functions that I want to call
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TrojanVirusDie : MonoBehaviour
    6. {
    7.   public GameObject VirusChild1;
    8.   public GameObject VirusChild2;
    9.   public GameObject VirusChild3;
    10.   public GameObject VirusChild4;
    11.   public GameObject VirusChild5;
    12.  
    13.   void Start()
    14.   {
    15.     VirusChild1.SetActive (false);
    16.     VirusChild2.SetActive (false);
    17.     VirusChild3.SetActive (false);
    18.     VirusChild4.SetActive (false);
    19.     VirusChild5.SetActive (false);
    20.   }
    21.  
    22.   public void ReleaseChildren()
    23.   {
    24.         Debug.Log ("The Trojan is releasing chickens");
    25.         VirusChild1.SetActive (true);
    26.         VirusChild2.SetActive (true);
    27.         VirusChild3.SetActive (true);
    28.         VirusChild4.SetActive (true);
    29.         VirusChild5.SetActive (true);
    30.   }
    31.  
    32.   public void DetachFromParent()
    33.   {
    34.           // Detaches the transform from its parent.
    35.           VirusChild1.transform.parent = null;
    36.           VirusChild2.transform.parent = null;
    37.           VirusChild3.transform.parent = null;
    38.           VirusChild4.transform.parent = null;
    39.           VirusChild5.transform.parent = null;
    40.           ReleaseChildren();
    41.   }
    42. }
    43.  

    the code below is used to kill the Trojan

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ParentHealth : MonoBehaviour
    4. {
    5.     // The amount of health the player starts the game with.
    6.     public int startingHealth = 100;
    7.     [HideInInspector] public int currentHealth;
    8.     // Whether the player is dead.
    9.     private bool isDead = false;
    10.  
    11.     [Header("Respawn (Optional)")]
    12.     public bool respawnInsteadOfDeath = false;
    13.     public GameObject gameObjectToDisable = null;
    14.     public float cooldownForRespawn = 0.5f;
    15.  
    16.     TrojanVirusDie Trojn = gameObject.GetComponent<TrojanVirusDie>();
    17.  
    18.  
    19.     protected void Awake()
    20.     {
    21.         // Set the initial health of the player.
    22.         currentHealth = startingHealth;
    23.     }
    24.  
    25.     public void TakeDamage(int amount)
    26.     {
    27.         // Reduce the current health by the damage amount.
    28.         currentHealth -= amount;
    29.  
    30.         // If the player has lost all it's health and the death flag hasn't been set yet...
    31.         if (currentHealth <= 0 && !isDead)
    32.         {
    33.             if (respawnInsteadOfDeath)
    34.             {
    35.                 DisableAndRespawn();
    36.             }
    37.             else
    38.             {
    39.                 Trojn.DetachFromParent();
    40.                 Death();
    41.             }
    42.         }
    43.     }
    44.  
    45.     public virtual void Death()
    46.     {
    47.         // Set the death flag so this function won't be called again.
    48.         isDead = true;
    49.         Debug.Log(string.Format("{0} is dead", name));
    50.         Destroy(gameObject);
    51.     }
    52.  
    53.     public void DisableAndRespawn()
    54.     {
    55.         // Disattivo GO, in futuro ci sarà un'animazione
    56.         gameObjectToDisable.SetActive(false);
    57.  
    58.         // Invoko il respawn
    59.         Invoke(nameof(Respawn), cooldownForRespawn);
    60.     }
    61.  
    62.     public void Respawn()
    63.     {
    64.         // Set the initial health of the player.
    65.         currentHealth = startingHealth;
    66.  
    67.         // Riattivo il GO
    68.         if (gameObjectToDisable != null)
    69.         {
    70.             gameObjectToDisable.SetActive(true);
    71.         }
    72.     }
    73. }
    74.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What's your question though?
     
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Looks like you don't call ReleaseChildren()
     
  4. FriedPollo

    FriedPollo

    Joined:
    Sep 24, 2020
    Posts:
    13
    the trojan dies but it doesn't release the children, I tried with a console log but it seems the function to release the children has not been called or it dies too quick before call it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ParentDie : MonoBehaviour
    6. {
    7.   public GameObject Child1;
    8.   public GameObject Child2;
    9.   public GameObject Child3;
    10.   public GameObject Child4;
    11.   public GameObject Child5;
    12.   public GameObject Child6;
    13.  
    14.   void Start()
    15.   {
    16.     Child1.SetActive (false);
    17.     Child2.SetActive (false);
    18.     Child3.SetActive (false);
    19.     Child4.SetActive (false);
    20.     Child5.SetActive (false);
    21.     Child6.SetActive (false);
    22.   }
    23.  
    24.   public void ReleaseChildren()
    25.   {
    26.         Debug.Log ("The Trojan is releasing chickens");
    27.         Child1.SetActive (true);
    28.         Child2.SetActive (true);
    29.         Child3.SetActive (true);
    30.         Child4.SetActive (true);
    31.         Child5.SetActive (true);
    32.         Child6.SetActive (true);
    33.   }
    34.  
    35.   public void DetachFromParent()
    36.   {
    37.           ReleaseChildren();
    38.           // Detaches the transform from its parent.
    39.           Child1.transform.parent = null;
    40.           Child2.transform.parent = null;
    41.           Child3.transform.parent = null;
    42.           Child4.transform.parent = null;
    43.           Child5.transform.parent = null;
    44.           Child6.transform.parent = null;
    45.   }
    46. }
    47.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ParentHealth : MonoBehaviour
    4. {
    5.     // The amount of health the player starts the game with.
    6.     public int startingHealth = 1;
    7.     [HideInInspector] public int currentHealth;
    8.     // Whether the player is dead.
    9.     private bool isDead = false;
    10.  
    11.     [Header("Respawn (Optional)")]
    12.     public bool respawnInsteadOfDeath = false;
    13.     public GameObject gameObjectToDisable = null;
    14.     public float cooldownForRespawn = 0.5f;
    15.     public ParentDie troj;
    16.  
    17.  
    18.     protected void Awake()
    19.     {
    20.         // Set the initial health of the player.
    21.         currentHealth = startingHealth;
    22.        troj  = gameObject.GetComponent<ParentDie>();
    23.  
    24.     }
    25.  
    26.     public void TakeDamage(int amount)
    27.     {
    28.         // Reduce the current health by the damage amount.
    29.         currentHealth -= amount;
    30.  
    31.         // If the player has lost all it's health and the death flag hasn't been set yet...
    32.         if (currentHealth <= 0 && !isDead)
    33.         {
    34.             if (respawnInsteadOfDeath)
    35.             {
    36.                 DisableAndRespawn();
    37.             }
    38.             else
    39.             {
    40.                 troj.DetachFromParent();
    41.                 Death();
    42.             }
    43.         }
    44.     }
    45.  
    46.     public virtual void Death()
    47.     {
    48.         // Set the death flag so this function won't be called again.
    49.         isDead = true;
    50.         Debug.Log(string.Format("{0} is dead", name));
    51.         Destroy(gameObject);
    52.     }
    53.  
    54.     public void DisableAndRespawn()
    55.     {
    56.         // Disattivo GO, in futuro ci sarà un'animazione
    57.         gameObjectToDisable.SetActive(false);
    58.  
    59.         // Invoko il respawn
    60.         Invoke(nameof(Respawn), cooldownForRespawn);
    61.     }
    62.  
    63.     public void Respawn()
    64.     {
    65.         // Set the initial health of the player.
    66.         currentHealth = startingHealth;
    67.  
    68.         // Riattivo il GO
    69.         if (gameObjectToDisable != null)
    70.         {
    71.             gameObjectToDisable.SetActive(true);
    72.         }
    73.     }
    74. }
    75.  
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Are you sure line 39 is even running? Have you tried Debug.Log there to make sure?

    Do you have any errors in the console?
     
    FriedPollo likes this.
  6. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    As I said ReleaseChildern() is never called so they will neither be set active nor will you see a Debug.Log() message when placed inside your ReleaseChildren()
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    He's calling ReleaseChildren inside DetachParent which is called on line 39 of ParentHealth. That's why I'm asking if line 39 is running.