Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eowyn27 · Jan 14, 2014 at 04:00 PM · cameraobjectssetactivegui.buttoninactive

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;
 

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 14, 2014 at 11:51 PM 0
Share

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.

avatar image Eowyn27 · Jan 15, 2014 at 04:36 PM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eowyn27 · Jan 14, 2014 at 09:31 PM 0
Share

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?

avatar image Fabkins · Jan 14, 2014 at 09:35 PM 0
Share

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.

avatar image Fabkins · Jan 14, 2014 at 09:40 PM 0
Share

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

avatar image Eowyn27 · Jan 15, 2014 at 04:35 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I find my inactive objects and set them active again? 3 Answers

Moving two objects and avoiding overlap with each other 1 Answer

Problem in camera while rendering some 3D objects..? 0 Answers

Multiple player objects with using one camera 1 Answer

physics.OverlapSphere colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges