New coder: question about activating a child object

Hello,
This is my first time in the Unity forums. I’m very new at programming and have been cutting/ pasting a lot to learn. I’ve put together the script below, but I need it to access the child object of “NewsPlayer,” called “NewsAudio.” I want the player to be able to turn the news audio on and off with the N key.

This script works with NewsPlayer when I had the audio in that object, but the problem was that the NewsPlayer object must begin in an activate state. When that’s the case, the player can deactivate and activate it whenever, but I don’t want the game to begin with the audio playing (I want the NewsPlayer to be inactive at start, but then the script can’t find it!). My solution was to put the audio in the child object “NewsAudio,” and leave NewsPlayer as an empty but always active parent.

QUESTION: How can I get this script to find the NewsPlayer object, then find its NewsAudio (deactivated) child, and activate / deactivate that child by pressing the N key?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewsToggle : MonoBehaviour
{
    private GameObject NewsPlayer;
    private bool NewsActive = true;


    // Update is called once per frame
    void Update()
    {
        if (FindNewsPlayer() && Input.GetKeyDown(KeyCode.N))
        {
            if (NewsActive)
            {
                NewsPlayer.SetActive(false);
                NewsActive = false;

            }
            else
            {
                NewsPlayer.SetActive(true);
                NewsActive = true;
            }
        }


    }


    public bool IsNewsActive()
    {
        return NewsActive;
    }

    private bool FindNewsPlayer()
    {
        if (NewsPlayer == null)
        {
            // Find the NewsPlayer
            NewsPlayer = GameObject.Find("NewsPlayer");

        }

        return NewsPlayer != null;
    }
}

Thanks in advance for any help!

Don’t use GameObject.Find.

Get rid of that FindNewsPlayer, it shouldn’t be needed. Make your private variable for the gameobject either public or use the [SerializeField] attribute in front of it so it shows in the inspector. Then hook it up in the scene.

Then, in a Start or Awake, I would use a playerpref check to see if the audio should be on or off with default to off. Your Update would then set this playerpref as well when doing the toggle.

Making it a public variable was such a simple and perfect solution, it works great! I don’t know why that didn’t occur to me, but probably because I’ve been focusing too much on scriptable objects lately.

Thanks for the help!