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?
Answer by Gianluca90 · Jul 01, 2016 at 09:58 AM
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:link text
Answer by hyperi0n · Jun 28, 2016 at 09:59 AM
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?
Ok I would suggest the following: attach a script to your BUTTON, call it "CycleThroughObjectsScript" or something ;) Inside the script declare an array of game Objects as well as an int variable:
public var objects : GameObject[]; public var currentObject : int;
In the editor, drag all the possible objects from the hierarchy into the appropriate slot in the btton's inspector window under the script. Note, that only one of the objects should be active.
then in the Start function of the script, I would place something like
for(var i : int = 0; i < objects.length; i++){
if (objects[i].activeSelf){
currentObject = i;
}
}
This is so that the script knows which object is currently being displayed. Then add a function to the script:
function DisplayNextObject(){
objects[i].SetActive(false);
i++;
if ( i > objects.length){
i = 0;
}
objects[i].SetActive(true);
}
One last thing left to do: In the editor, select the button, and in the inspector go to the bottom where it says "OnClick". ins$$anonymous$$d of the things we added there in my original reply, simply have one effect (click the "+" once) and do the following: drag the SCRIPT component into the "object" slot. Then, in the "function" dropdown, choose "CycleThroughObjectsScript" (or whatever you named the script) and select the "DisplayNextObject" function. If everything went well, this should allow you to cycle through the 3d models with 1 button :)