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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Setting a Child as a Gameobject

Discussion in 'Scripting' started by RaGEn, Aug 26, 2017.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Hello

    I know this is probably very easy, but I've been stuck on this for awhile now. I'm trying to add a child to a gameobject variable in a script. Basically I'm doing this so I can only allow one voiceline from a character to play at once. So I want to have it so when a gameobject called Death Voiceline spawns and after it's added to the empty gameobject it is set as the variable I have set. And if that variable has an object in it. The newly added voice line is destroyed.

    I can't figure out how to add the child to the variable however.

    Code (CSharp):
    1. public GameObject VoicePlaying;
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.        
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     void Update ()
    10.     {
    11.         if (VoicePlaying != null)
    12.         {
    13.             VoicePlaying = gameObject.transform.FindChild ("Death Voiceline").gameObject;
    14.         }
    15.  
    16.     }
    This is what I'm doing. Anyone have any thoughts?

    Thank you
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Well...FindChild is deprecated, but it might still work, however, I wouldn't use it.

    However, two issues.

    First, doing this in update is bad. If you are instantiating something, you should assign it then. Otherwise, you will constantly be doing this call over and over again.

    Second, your check is for VoicePlaying to not be null, which means once something is assigned to it, you want to assign something else to it? I'm not even sure I understand what you are really trying to do to be honest.
     
    RaGEn likes this.
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is my two cents

    Code (CSharp):
    1. //what gameObect has this script?  I assume the parent
    2.  
    3. public GameObject VoicePlaying; //don't set this via the inspector.  I would like to see it as a private
    4.     // Use this for initialization
    5.     void Start () {
    6.      
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update ()
    11.     {
    12.         if (VoicePlaying != null) //if it's not null
    13.         {
    14.             // it's assigned so do something with it every frame
    15.         }else
    16.         {
    17.             //reassign VoicePlaying to use child gameObect
    18.             VoicePlaying = GameObject.Find(gameObject.name + "/Death Voiceline"); //find and set the child via it's name
    19.             //you can know do some more code here, like set a value in the child
    20.             VoicePlaying.GetComponent<ChildScriptBlaBla>().somevariable = something;
    21.         }
    22.     }
     
    RaGEn likes this.
  4. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thanks for the reply, sorry I didn't make much sense there, I was off today haha.I figured out the issue, thanks for your feedback it got me to think of the answer. With telling me that it was a bad idea to use the update.
     
  5. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thanks for the reply, that is very useful information. Thank you for taking the time to answer my question!