- Home /
SetActive() Method is best way to do this Guys. Thanks to Andres
Destroying Objects.
Hi all, i have load multiple objects in my scene. All i have to do is load objects when i press the object icon, i have created this script for instantiate the objects.
using UnityEngine;
using System.Collections;
public class Model : MonoBehaviour
{
public Transform Table, Chair, Door;
private Transform Table_Temp, Chair_Temp, Door_Temp;
public GUITexture Chair_Button, Table_Button, Door_Button;
private bool Chair_Click = false, Table_Click = false, Door_Click = false;
void Update()
{
if(Input.touchCount > 0)
{
if(Chair_Button.HitTest (Input.GetTouch(0).position))
{
Chair_Click = true;
if(Chair_Click == true)
{
Chair_Temp = Instantiate (Chair, new Vector3 (0, 0, 0), Quaternion.identity)as Transform;
Chair_Click = false;
}
if(Chair_Click == false)
{
Destroy (Chair_Temp);
}
}
else if (Table_Button.HitTest (Input.GetTouch (0).position))
{
Table_Click = true;
if(Table_Click == true)
{
Table_Temp = Instantiate (Table, new Vector3 (0, 0, 0), Quaternion.identity)as Transform;
Table_Click = false;
}
if(Table_Click == false)
{
Destroy (Table_Temp);
}
}
else if(Door_Button.HitTest (Input.GetTouch(0).position))
{
Door_Click = true;
if(Door_Click == true)
{
Door_Temp = Instantiate (Door, new Vector3(0, 0, 0), Quaternion.identity)as Transform;
Door_Click = false;
}
if(Door_Click == false)
{
Destroy(Door_Temp);
}
}
}
}
}
Here its instantiate the objects when i press the icon, but its not destroying. Which means i want to show only one object at runtime, if i press 1st button my first object only will instantiate same as 2 and 3.
Answer by Andres-Fernandez · Dec 02, 2014 at 07:32 AM
Do not check for the Chair_click == false (or the other checks) inside the if(Chair_Button.HitTest (Input.GetTouch(0).position)) statement because it'll never be false (you've just set it true!). Do that outside the if(Input.touchCount > 0) statement.
void Update()
{
if(Input.touchCount > 0)
{
if(Door_Button.HitTest (Input.GetTouch(0).position))
{
Door_Click = true;
}
// Other hit tests...
}
if(Door_Click == true)
{
Door_Temp = Instantiate (Door, new Vector3(0, 0, 0), Quaternion.identity)as Transform;
Door_Click = false;
}
if(Door_Click == false)
{
Destroy(Door_Temp);
}
// Other _Click = true/false checks...
}
And another suggestion. Don't instantiate and destroy. Create the objects inside start function and then use SetActive().
The SetActive is used correctly, although you should check if the object exists before using the SetActive function, just in case it was not created correctly. Just make sure that you are instantiating the objects correctly. Have you debugged the code? I suggest you debug that Start function to check if you are instantiating all objects correctly. Do all three objects get instantiated? Do you get any error?
You should also check for the number of touches before accesing them (as you did in the code of the question).
I suggest you polish the logic of the SetActive in editor mode, with the mouse (or the Unity Remote) before jumping to the device and the touches.
Sorry, actually that's worked for me. i used this script to instantiate models.
public GameObject Table, Chair, Door;
private GameObject Table_Temp, Chair_Temp, Door_Temp;
public GUITexture Chair_Button, Table_Button, Door_Button;
void Start()
{
Chair_Temp = Instantiate (Chair, new Vector3(0, 0, 0), Quaternion.identity)as GameObject;
Table_Temp = Instantiate (Table, new Vector3(0, 0, 0), Quaternion.identity)as GameObject;
Door_Temp = Instantiate (Door, new Vector3(0, 0, 0), Quaternion.identity)as GameObject;
}
void Update()
{
if(Chair_Button.HitTest (Input.GetTouch(0).position))
{
Chair_Temp.SetActive (true);
Table_Temp.SetActive (false);
Door_Temp.SetActive (false);
}
if(Table_Button.HitTest (Input.GetTouch(0).position))
{
Chair_Temp.SetActive (false);
Table_Temp.SetActive (true);
Door_Temp.SetActive (false);
}
if(Door_Button.HitTest (Input.GetTouch(0).position))
{
Chair_Temp.SetActive (false);
Table_Temp.SetActive (false);
Door_Temp.SetActive (true);
}
}
here is used instantiate at start function. So i get all objects in same position like one after one, i don't want that
Well, you only need to change the new Vector3(0,0,0) for the desired position.