Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Dec 03, 2014 at 04:56 AM by Prasanna for the following reason:

SetActive() Method is best way to do this Guys. Thanks to Andres

avatar image
0
Question by Prasanna · Dec 02, 2014 at 07:11 AM · c#instantiateprefabdestroy

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.

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

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().

Comment
Add comment · Show 3 · 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 Andres-Fernandez · Dec 02, 2014 at 09:17 AM 0
Share

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.

avatar image Prasanna · Dec 02, 2014 at 09:22 AM 0
Share

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

avatar image Andres-Fernandez · Dec 02, 2014 at 03:10 PM 0
Share

Well, you only need to change the new Vector3(0,0,0) for the desired position.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Issue Instantiating prefab in C# 0 Answers

Issue Instantiating prefab in C# 2 Answers

i want to assign different colors to rows or different colors to the brick? 0 Answers

How to store a prefab in a variable 1 Answer

Trouble with destroying an instantiated prefab 2 Answers


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