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

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! :slight_smile:

gameObject

Spelling is important. :wink:

@zombiegorilla can you help? like I mean respond with code please.

Like I said, gameObject not GameObject.

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

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.

I did as you instructed @RickyX but I got the following error.

Here is my code

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckAnsL1 : MonoBehaviour
{
   
    public InputField iField;
    string myName;
    string myText;
    public int score = 0;

    public GameObject theTargetChild;
   
    GameObject myTextgameObject; // gameObject in Hierarchy
    Text ourComponent;           // Our refference to text component
   
    void Start()
    {
        if (PlayerPrefs.HasKey("Score") == true)
        {
            score = PlayerPrefs.GetInt("Score");
        }
       
        // Find gameObject with name "MyText"
        myTextgameObject = GameObject.Find("MyText");
        // Get component Text from that gameObject
        ourComponent = myTextgameObject.GetComponent<Text>();
    }
   
    public void MyFunction()
    {
        Debug.Log(iField.text);
        myName = iField.text;
        if (myName == "city")
        {
            score++;
            Debug.Log("Correct! The word 'city' is correct!");
            Debug.Log("Your score is now:");
            Debug.Log(score);
            PlayerPrefs.SetInt("Score", score);
        }
        else
        {
            score--;
            Debug.Log("Incorrect! The answer was 'city'.");
            Debug.Log("Your score is now:");
            Debug.Log(score);
            PlayerPrefs.SetInt("Score", score);
        }

        theTargetChild.setActive(True);
    }
   
    void Update () {
        ourComponent.text = score.ToString();
    }
}

And here is a screenshot of the hierarchy…

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

Please tell me what I am doing wrong :slight_smile:

@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.

@zombiegorilla & @RickyX thanks for your help! I fixed all the errors myself! So proud! :slight_smile: I thank you for your help so far and will contact you if needed in the future! Thanks again!

Sorry pal’ I wasn’t around, i’d help. Glad you fixed it though !

Sorry didn’t know about that post…

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…

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckAnsL8 : MonoBehaviour
{
   
    public InputField iField;
    string myName;
    string myText;
    public int score = 0;
   
    public Text Status;
   
    GameObject myTextgameObject; // gameObject in Hierarchy
    Text ourComponent;           // Our refference to text component
   
    void Start()
    {
        if (PlayerPrefs.HasKey("Score") == true)
        {
            score = PlayerPrefs.GetInt("Score");
        }
       
        // Find gameObject with name "MyText"
        myTextgameObject = GameObject.Find("MyText");
        // Get component Text from that gameObject
        ourComponent = myTextgameObject.GetComponent<Text>();
    }
   
    public void MyFunction()
    {
        Debug.Log(iField.text);
        myName = iField.text;
        if (myName == "castle")
        {
            score++;
            Debug.Log("Correct! The word 'castle' is correct!");
            Debug.Log("Your score is now:");
            Debug.Log(score);
            PlayerPrefs.SetInt("Score", score);
            Status.text = "Correct!";
            Status.color = Color.green;
        }
        else
        {
            score--;
            Debug.Log("Incorrect! The answer was 'castle'.");
            Debug.Log("Your score is now:");
            Debug.Log(score);
            PlayerPrefs.SetInt("Score", score);
            Status.text = "Incorrect!";
            Status.color = Color.red;
        }
       
        GameObject theTargetChild = GameObject.Find("theTargetChild");
        foreach (Transform child in theTargetChild.transform)
        {
            child.gameObject.SetActive(true);
        }
    }
   
    void Update () {
        ourComponent.text = score.ToString();
    }
}

FIXED IT! YAY