- Home /
Setting inactive objects as active after click on gui button?
Right now, I have a scene with a main camera focused on a huge complex organization of blocks. I have wired the camera to move and zoom into the one of the blocks but inactivating the other objects when it does so (So the other blocks do not show when zooming in). Now, I want to move my camera back the previous scene with the complexity of the blocks and render all the blocks as active once more. However, for some reason, when I go back to where the main camera was initially (zoomed out), the objects are still inactive. I'm not sure what's wrong with my code? Here's part of the script for changing views to where the objects were located.
using UnityEngine;
using System.Collections;
public class BackButton : MonoBehaviour {
public Camera mainCam;
public GameObject[] Objects;
void OnGUI () {
// Make a background box
GUI.Box(new Rect(10,10,250,200), "Menu");
if(GUI.Button (new Rect (30,40,200,70), "Back to Blocks with stripes ")) {
print ("You clicked the button!");
mainCam.transform.position= new Vector3(-.13f, 0.87f, -8);
Camera.main.orthographicSize = 0.4f;
}
if (GUI.Button (new Rect (30,120,200,70), "Back to the Block Complex")) {
print ("You clicked the button!");
Objects = GameObject.FindGameObjectsWithTag("blocks");
for (int i=0; i<Objects.Length; i++) {
Objects[i].active=true;
}
mainCam.transform.position= new Vector3(0, 1.1f, -8);
Camera.main.orthographicSize = 1.3f;
}
}
}
I tried to find all the objects tagged with blocks and setting them back to active (after I have set them inactive after mousedown on one of the blocks) but it doesn't seem to work.
When I comment out this part, the same thing happens. So I think this part of the script isn't doing it's job:
Objects = GameObject.FindGameObjectsWithTag("blocks");
for (int i=0; i<Objects.Length; i++) {
Objects[i].active=true;
}
As an alternate approach to what is suggested, how about not deactivating the blocks but simply turning their renderers off. Depending on what happens while you are zoomed in, you may have to turn of other components as well (like colliders). But since the game objects are not disabled, you will be able to use GameObject.FindGameObjectsWithTag() to find them.
Yes, I realized disabling the game object was a horrible idea to begin with if I was to try to detect them later on. I will try to adjust everything by setting renderer.enabled =false next time. Thanks for the tip!
Answer by Fabkins · Jan 14, 2014 at 09:16 PM
The problem is because once you have made the object inactive, you can no longer find it with the FindGameObjectsWithTag.
The answer for you however is easy, you ALREADY got the list of objects as you defined Objects globally. So all you need to do is remove the lookup in the section where you are re-activating the objects.
eg:
/* NOT NEEDED Objects = GameObject.FindGameObjectsWithTag("blocks"); */
for (int i=0; i<Objects.Length; i++) {
Objects[i].active=true;
}
But there's nothing currently in the array of objects. Objects isn't really defined to contain my blocks. Objects is not connected to Block1, Block2, Block3. Do you mean I should set Public GameObject Block1 = Objects[0] and slot them into the array?
Create a NEW class that has two static functions, one for disabling the blocks and one for enabling the blocks. That class will have the global declaration for the block list.
Those utility function can then be called in places you want the objects enabled or disabled.
An alternative is create a new layer for your blocks and make the camera exclude that layer when its rendering the zoom action.
http://docs.unity3d.com/Documentation/Components/class-Tag$$anonymous$$anager.html
I figured out an easy lazy fix in which my object had children with the names Cube. So I used:
if(GameObject.Find("Cube")){ // found one! set object to inactive, or disable the renderer, as described above Cube.SetActive(false); }
And it worked. When I get the chance, I will refactor everything and try to pick everything in one class. Thank you!! :D