- Home /
Rubiks Cube - Inactivate child object after parent instantiation
Hello,
I want to build a (3x3x3) 3D Rubiks Cube in which 27 SubCubes are incorporated, which works perfectly so far. SubCube (prefab) is a black (1x1x1) cube and has 6 colored planes as child objects for each side of SubCube.
In my CubeBuilder-Script I want to instantiate this SubCube 27 times, placing SubCubeClone at the right location and change its child objects (the colored planes) to inactivate redundant colored sides of SubCube. Latter isn't working properly.
Redundant are those sides, which are NOT on the surface of the Rubiks Cube. So according to the location of the SubCube I want to inactivate some child objects of SubCube. Here is the important part of CubeBuilder-Script:
void CreateCube()
{
for (int x = 0; x < 3; ++x)
{
for (int y = 0; y < 3; ++y)
{
for (int z = 0; z < 3; ++z)
{
GameObject piece = Instantiate(SubCube, transform.position, transform.rotation);
piece.GetComponent<SubCubeBuilder>().ResetColor(); // inactivate all planes
piece.GetComponent<SubCubeBuilder>().SetColor(-x, -y, z); // set appropriate planes
piece.transform.localPosition = new Vector3(-x, -y, z);
}
}
}
}
Part of SubCubeBuilder-Script:
public GameObject UpSide, FrontSide, LeftSide, BackSide, RightSide, DownSide;
public void SetColor(int x, int y, int z)
{
if (x == 0)
FrontSide.SetActive(true);
else if (x == -2)
BackSide.SetActive(true);
if (y == 0)
UpSide.SetActive(true);
else if (y == -2)
DownSide.SetActive(true);
if (z == 0)
LeftSide.SetActive(true);
else if (z == 2)
RightSide.SetActive(true);
}
public void ResetColor()
{
UpSide.SetActive(false);
FrontSide.SetActive(false);
LeftSide.SetActive(false);
BackSide.SetActive(false);
RightSide.SetActive(false);
DownSide.SetActive(false);
}
In Unity: An empty GameObject called Cube gets CubeBuilder-Script in which SubCube is attached to. The SubCube-Prefab gets SubCubeBuilder-Script, in which the 6 colored plane-prefabes (UpSide, FrontSide, .., DownSide) are attached to.
When I hit play in Unity, the cube gets created, but EVERY SubCube still has ALL colored planes activated. Just in the Prefabs folder UpSide, LeftSide and FrontSide are inactivated (which should correspond to the last SubCube added to the Rubiks Cube), but the actual Rubiks cube is unaffected by these changes.
So it seems like ResetColor() and SetColor() know what they need to do, but after the instantiation of SubCube somehow changing of child objects of SubCube doesn't get applied in the final Rubiks Cube.
How can I fix this problem? What am I doing wrong?
Thank you in advance!
Answer by unity_ek98vnTRplGj8Q · Dec 18, 2019 at 03:53 PM
In your sub cube prefab where you have your references to UpSide, FrontSide, LeftSide, etc. make sure these are to prefab instances, the objects that are actually in the scene hierarchy, rather than references to the prefab objects themselves in your project explorer
Answer by maxbionade · Dec 18, 2019 at 05:37 PM
Thanks for your answer. Actually all 6 Sides are prefabs. And I attached these to SubCubeBuilder-Script in the SubCube-Prefab.
I don't get it. Somehow it works now. I created a new SubCube-Prefab and ins$$anonymous$$d of attaching the plane-prefabs from Prefabs folder to SubCube in Prefabs folder, I attached the plane-prefabs from Prefabs folder to SubCube in the game scene. After that, the prefabs symbol had a "+"-sign, whatever this means. Obviously I have problems understanding this kind of stuff. Can anyone explain this context or give me some links according to my problem to understand whats going on?
Check out https://unity.com/prefabs. Unity recently improved the prefabs system, and you can find most of the vital info here. To answer your question: the "+" symbol next to a gameobject will appear in the scene if that gameobject is the child of a prefab in the scene but not a child of the prefab overall. When you are editing prefabs, the best place to do it is in the prefab editor. Click on the prefab in your folder and click "Open Prefab" in the inspector. In your case, make sure that when you open your SubCube prefab in the prefab editor it has each plane prefab as a child of it already. It is these gameobjects (the ones in the prefab editor hierarchy) that you want to drag into the reference slots, NOT the prefabs that they come from in your folder.