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!