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

How to activate children in heirachry from a script not attached to that gameobject?

Discussion in 'Editor & General Support' started by MattCarter24, Jun 24, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I have tried this code....

    child.GameObject.setActive (true);

    or something like it. But I know whatever it was is correct for usual circumstances.

    I have just removed the code as it doesn't work for the circumstance I wish it to work for. It just creates compiler errors (I apologise for not uploading them but I didn't see the point as I first need to adjust this code to somehow access hierarchy components when not attached to them through a pre-existing script). I am trying to activate a child named "Button1" in a gameobject called "GameObject2". How can I do this from a script which IS NOT attached to either of these items as it is attached to another button that will run when clicked?

    Thanks - Any help would be much appreciated! :)
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    gameObject

    Spelling is important. ;)
     
  3. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @zombiegorilla can you help? like I mean respond with code please.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Like I said, gameObject not GameObject.
     
  5. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    This is a very simple thing, and @zombiegorilla it's not that, do you even understand what he needs ?

    So you want to activate the child of some object, but from a script that is attached to a whole diffrent object which is neither of theese two...

    This can't be more simple, here's a C#:

    public GameObject theTargetChild;


    And then add this code to activate it:

    theTargetChild.setActive(True);


    EDIT:
    Sorry, and then back at unity screen, take a look at your script, and you should see a new value added called "The Target Child". Now drag and drop your child object into that value, and you should be good to go.

    EDIT 2: If you're not using C#, this won't work... And really can't help you if you're scripting in javascript, i haven't used that in a long time and i'm not really sure there.


    Aaand, if you need a full code, a whole piece of code then tell me what you exactly want to happen, maybe a button press will activate that child or something idk, but i think this should help,



    EDIT 3: Maaybe this could work in javascript, i'm not sure, you can try it:

    var theTargetChild: GameObject;

    And then to activate it:

    theTargetChild.setActive(True);


    And then back at unity do the same i said before
     
    Last edited: Jun 24, 2015
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Yes, because he has already been given the answer, this is a double post.
    http://forum.unity3d.com/threads/please-help-beginner-overall-help.328603/

    "child" is a Transform, the result of a looping through an array of Transform. GameObject is a class, gameObject is a member of Transform that returns the GameObject the Transform is attached to. .SetActive is a method of GameObject.
    So:
    child.gameObject.SetActive(bool) sets the active state of the GameObject "child" is attached to.
    child.GameObject.SetActive(bool) is gibberish and will throw an error.
     
  7. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    I did as you instructed @RickyX but I got the following error.

    upload_2015-6-24_21-47-11.png

    Here is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL1 : MonoBehaviour
    5. {
    6.    
    7.     public InputField iField;
    8.     string myName;
    9.     string myText;
    10.     public int score = 0;
    11.  
    12.     public GameObject theTargetChild;
    13.    
    14.     GameObject myTextgameObject; // gameObject in Hierarchy
    15.     Text ourComponent;           // Our refference to text component
    16.    
    17.     void Start()
    18.     {
    19.         if (PlayerPrefs.HasKey("Score") == true)
    20.         {
    21.             score = PlayerPrefs.GetInt("Score");
    22.         }
    23.        
    24.         // Find gameObject with name "MyText"
    25.         myTextgameObject = GameObject.Find("MyText");
    26.         // Get component Text from that gameObject
    27.         ourComponent = myTextgameObject.GetComponent<Text>();
    28.     }
    29.    
    30.     public void MyFunction()
    31.     {
    32.         Debug.Log(iField.text);
    33.         myName = iField.text;
    34.         if (myName == "city")
    35.         {
    36.             score++;
    37.             Debug.Log("Correct! The word 'city' is correct!");
    38.             Debug.Log("Your score is now:");
    39.             Debug.Log(score);
    40.             PlayerPrefs.SetInt("Score", score);
    41.         }
    42.         else
    43.         {
    44.             score--;
    45.             Debug.Log("Incorrect! The answer was 'city'.");
    46.             Debug.Log("Your score is now:");
    47.             Debug.Log(score);
    48.             PlayerPrefs.SetInt("Score", score);
    49.         }
    50.  
    51.         theTargetChild.setActive(True);
    52.     }
    53.    
    54.     void Update () {
    55.         ourComponent.text = score.ToString();
    56.     }
    57. }
    And here is a screenshot of the hierarchy...

    upload_2015-6-24_21-48-35.png

    Please tell me what I am doing wrong :)
     
  8. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @zombiegorilla Can you find the issue in the post above by me? I have provided the code, an image of my hierarchy and even the compiler errors.
     
  9. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @zombiegorilla & @RickyX thanks for your help! I fixed all the errors myself! So proud! :) I thank you for your help so far and will contact you if needed in the future! Thanks again!
     
  10. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Sorry pal' I wasn't around, i'd help. Glad you fixed it though !
     
  11. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Sorry didn't know about that post...
     
  12. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I have the code below which works perfectly. I would like to somehow (in a new scene) pull the score variable from the previous code and display it in a UI text. I would also like the new script in the new scene to run automatically without any triggers?

    Any help would be much appreciated!

    Here is my code...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL8 : MonoBehaviour
    5. {
    6.    
    7.     public InputField iField;
    8.     string myName;
    9.     string myText;
    10.     public int score = 0;
    11.    
    12.     public Text Status;
    13.    
    14.     GameObject myTextgameObject; // gameObject in Hierarchy
    15.     Text ourComponent;           // Our refference to text component
    16.    
    17.     void Start()
    18.     {
    19.         if (PlayerPrefs.HasKey("Score") == true)
    20.         {
    21.             score = PlayerPrefs.GetInt("Score");
    22.         }
    23.        
    24.         // Find gameObject with name "MyText"
    25.         myTextgameObject = GameObject.Find("MyText");
    26.         // Get component Text from that gameObject
    27.         ourComponent = myTextgameObject.GetComponent<Text>();
    28.     }
    29.    
    30.     public void MyFunction()
    31.     {
    32.         Debug.Log(iField.text);
    33.         myName = iField.text;
    34.         if (myName == "castle")
    35.         {
    36.             score++;
    37.             Debug.Log("Correct! The word 'castle' is correct!");
    38.             Debug.Log("Your score is now:");
    39.             Debug.Log(score);
    40.             PlayerPrefs.SetInt("Score", score);
    41.             Status.text = "Correct!";
    42.             Status.color = Color.green;
    43.         }
    44.         else
    45.         {
    46.             score--;
    47.             Debug.Log("Incorrect! The answer was 'castle'.");
    48.             Debug.Log("Your score is now:");
    49.             Debug.Log(score);
    50.             PlayerPrefs.SetInt("Score", score);
    51.             Status.text = "Incorrect!";
    52.             Status.color = Color.red;
    53.         }
    54.        
    55.         GameObject theTargetChild = GameObject.Find("theTargetChild");
    56.         foreach (Transform child in theTargetChild.transform)
    57.         {
    58.             child.gameObject.SetActive(true);
    59.         }
    60.     }
    61.    
    62.     void Update () {
    63.         ourComponent.text = score.ToString();
    64.     }
    65. }
     
  13. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    FIXED IT! YAY