- Home /
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 .
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 ) ;
}
Would you please help me with this error alone ?
Thanks in advance :)
Answer by misher · Aug 27, 2018 at 09:03 AM
If you want to avoid custom scripting, you can go with this setup:
Create 2 UI Toggles
For each Toggle in OnValueChanged Event select desired target GameObject, select SetActive dynamic method
Add Toggle Group component to the parent of 2 toggles
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).
but the thing is in one toggle button itself i have to hide a 3d object and show another 3d object .
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
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
}
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
Follow this Question
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