Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 sam_sundar · Aug 27, 2018 at 02:55 AM · vuforiaaugmented-realityvisibility

Script to Show one game object and hide another when button is pressed

Hi everyone , im developing a simple AR How to enable visibility of an Object and Hide visibility of another object upon pressing a single button ? So when image target is found , First 3d object will be shown and when button is pressed , Second 3d object will be shown and first one will hide .

Comment
Add comment
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

3 Replies

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

Answer by Hellium · Aug 27, 2018 at 02:18 PM

The following script must be attached to only one gameObject (your button for instance)

 // Drag & Drop your gameObjects here
 public GameObject[] GameObjectsList ;
 private int shownGameObjectIndex = -1 ;
 
 private void Start()
 {
     for( int i = 0 ; i < GameObjectsList.Length ; ++i )
         GameObjectsList[i].SetActive( false ) ;

     SelectNextGameObject();
 }

 // Add this function as callback for your button's onClick event
 public void SelectNextGameObject()
 {
     int index = shownGameObjectIndex >= GameObjectsList.Length - 1 ? -1 : shownGameObjectIndex ;
     SelectGameObject( index + 1 );
 }

 public void SelectPreviousGameObject()
 {
     int index = shownGameObjectIndex <= 0 ? GameObjectsList.Length : shownGameObjectIndex ;
     SelectGameObject( index - 1 );
 }

 public void SelectGameObject( int index )
 {
     if ( shownGameObjectIndex >= 0 )
         GameObjectsList[shownGameObjectIndex].SetActive( false );
     shownGameObjectIndex = index;
     GameObjectsList[shownGameObjectIndex].SetActive( true );
 }


If you want to use a toggle (so if you want to have only 2 gameObjects)

 public GameObject GameObject1;
 public GameObject GameObject2;

  // Add the following function to the toggle's event
 public void OnToggleValueChanged( bool value )
 {
     GameObject1.SetActive( value ) ;
     GameObject2.SetActive( !value ) ;
 }
Comment
Add comment · Show 1 · 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 sam_sundar · Aug 27, 2018 at 04:21 PM 0
Share

Would you please help me with this error alone ?

Thanks in advance :)

avatar image
2

Answer by misher · Aug 27, 2018 at 09:03 AM

If you want to avoid custom scripting, you can go with this setup:

  1. Create 2 UI Toggles

  2. For each Toggle in OnValueChanged Event select desired target GameObject, select SetActive dynamic method

  3. Add Toggle Group component to the parent of 2 toggles

  4. For each Toggle reference this ToggleGroup in Group

Now every time you check the other toggle, the GameObject it commands will become "active" (visibile), and another toggle in group will become unchecked and therefore its commanded GameObject will become "non active" (invisibile).

Comment
Add comment · Show 2 · 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 sam_sundar · Aug 27, 2018 at 10:49 AM 0
Share

but the thing is in one toggle button itself i have to hide a 3d object and show another 3d object .

avatar image misher sam_sundar · Aug 27, 2018 at 11:12 AM 1
Share

If you assign all your toggles to the same toggle group, once you check a toggle all other toggles in group become "unchecked" ans therefore they will "turn off" gameobjects they command

avatar image
1

Answer by Liquicidize · Aug 27, 2018 at 11:10 AM

You could try enabling/disabling the 3D objects.

 public gameObject object1;
 public gameObject object2;
 
 public void buttonClicked(){
        object1.setActive(false); //deactivates first object
        object2.setActive(true); //activates second object
 }

 
Comment
Add comment · Show 1 · 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 sam_sundar · Aug 27, 2018 at 02:06 PM 0
Share

Tried it and only One object is being shown from start and the other object is not showing even when the button is being clicked . What i did was , create a C# script , Attach it to empty object . Attach two 3d objects to script . Add the emptyobject to onclicked event and in function selected buttonclicked(). thing is when played only one object is shown but when button is clicked , nothing happens . :( and i thing im going to use toggles ins$$anonymous$$d of buttons . Any help please ?

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

102 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cloud recognition in Vuforia 0 Answers

How can I make android app run with less than 20 fps 2 Answers

Rendering lights and shadows in AR Vuforia 0 Answers

OnMouseDown not work in Vuforia 0 Answers

Unity and Vuforia 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