Swap 3D model with a button

Hello everybody! At first, sorry for my bad english. I’m working with unity 5.3.5 and Vuforia 5.5. Unfortunately i’m not able to write script then i’m in trouble!! I’m building an AR app for Android and i want to swap the 3d model with another 3d model when i press a GUI button. i’ve read
in all forums but nothing work fine… someone can help me?

2 Answers

2

Hi! If I understood you correctly, the easiest way would be:

Create/Load both objects at the start of the program and deactivate one of them. Then create a button for each of your objects.

Select your button. In the inspector, under the section “On Click()” click the little “+” sign at the bottom. Drag your first object into the “Object” Slot. In the “Function”-drop down, select GameObject/SetActive(Bool). A little checkbox appears; leave it empty. Do the same thing again (click plus, drag the second object into the object-slot; select “GameObject/SetActive(Bool)” and this time, activate the checkbox.

This way, when you click the button, Object A gets deactivated and Object B gets activated.
However, this will only work “one way”, so you will need one button per possible object. If you want to toggle/switch between the objects multiple times using one button, you will have to write a little script. If you need help with that, don’t hesitate to ask.

I hope this helped you a bit - good luck!

Hi hyperi0n! your method is good :) But if i want to use a lot of 3d models i need a script. i'd like to know how i can change this models with back and forth buttons. Can you help me with a script?

You just put a bool in your class to prevent the OnTriggerStay event from happening more than once. i.e.: class MyClass { bool doOnce = false; void OnTriggerStay(Collider other) { if (doOnce) return; doOnce = true; // The rest of your code that will only fire once. } } And if you need to reset it, just decide what should do that. It's a very common thing to do in game programming.

I solved my problem!I modified this script(https://www.ourtechart.com/augmented-reality/tutorial/augmented-reality-user-interface-unity3d/) adding features. This is the modified script:[73292-script.txt|73292]

@hyperi0n

A simple doOnce boolean won't do it : there is multiple other objects and fireballs , so it imply to store a list instead. I could de that, but I think it's easier in my case to have an AreaDamage children object (that can be also used for other things that do damage in an area)