TL;DR
I’ve got a simple mockup of my house in Unity and I want to press a key and have the kitchen change from the current kitchen to different kitchen. A simple “hide/show” game objects function.
In my project, the current kitchen is made of a couple Empty Game Objects that I use to group a few sets of objects. The new kitchen is also composed of different groups of objects, in a similar manner.
I have tagged the new kitchen “groups" (meaning empty game objects with objects inside) with a tag and the current kitchen groups with a different tag.
At first I thought I could simply make the tagged objects active or inactive, but I learned that an inactive object can’t be found with tags. Then I learned about disabling the render for certain objects. This worked on a basic test I did with primitives, but found it didn’t work with my object groups because they technically don’t have a mesh in the first place! (This is the script I attached)
I can’t find a solution to this simple problem anywhere!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KitchenSwitch : MonoBehaviour {
//declares the GameObjects as being public
public GameObject[] currentKitch;
public GameObject[] Kitch2;
public Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = false;
//Start Kitch2 OFF
Debug.Log ("Kitch2 OFF");
foreach(GameObject go in Kitch2)
{
go.GetComponent<MeshRenderer>().enabled = false;
}
}
void Update ()
{
//assigns values to currentKitch and Kitch2 as groups of objects that have the appropriate tags
GameObject[] currentKitch = GameObject.FindGameObjectsWithTag ("currentKitchTag");
GameObject[] Kitch2 = GameObject.FindGameObjectsWithTag ("Kitch2Tag");
// if you press E
if (Input.GetKeyDown(KeyCode.E))
{
//Turn Kitch2 ON
Debug.Log ("Kitch2 ON");
foreach(GameObject go in Kitch2)
{
go.GetComponent<MeshRenderer>().enabled = true;
}
//Turn currentKitch OFF
Debug.Log ("current kitch off");
foreach(GameObject go in currentKitch)
{
go.GetComponent<MeshRenderer>().enabled = false;
}
}
}
}
I’m going to suggest that you could simplify all of this by enabling and disabling a root game object, rather than a collection of renderers.
Consider placing all of the Kitchen1 meshes under an empty Kitchen1 game object. Then put all of the Kitchen2 meshes under an empty Kitchen2 game object.
Your update scares me. Is there a reason you want to search for all gameobjects with a tag every frame?
Also note that you have a public array, but then you are creating a local array. Which means your update arrays are not the same arrays that you declared at the start of your script. Which is why things don’t turn back on, you’re trying to find the objects again, but if FindGameObjectsWithTag aren’t finding things, it’s not going to work.
Now, I don’t know how you have things setup. But if you have a single empty gameobject that is a parent of all your kitch2 objects, you can create a reference to that parent and just turn it off or on for that group of kitchen objects. If you need the array, you can use the array and fill it with all kitchen objects and then loop through it when you need to. ( @kru beat me to this part)
kru, Thank you for your reply! I will try that then.
But the reason I had them in different heirarchies was that the models are exported from a sketchup file. So when I edit the sketchup model and tweak things, I just export a new FBX and replace it. Then simply re-tag the sub-objects inside that Sketchup FBX. Instead of having to break the sketchup prefab apart, drag and restructure everything each time I tweak the sketchup model.
I was just trying to make things simpler to tweak and update by using the tag option, but if I cant do it that way its not a huge deal.
Brathnann, thank you, I didnt realize I was referencing different Arrays like that. I’ve been reading this Unity C# book, watching tuts and trying some basic scripts of my own, but I am still missing things clearly. This is my first script language so its a lot to understand
EDIT: But I do try my best to search the forums and read documentation before asking my noob questions! but I still get stuck at times
Of course. That’s what we are here for. Everyone has to start somewhere and it’s always good to try something and then ask questions if you can’t understand why it’s not giving you the results you expected.