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 /
avatar image
0
Question by davidflynn2 · May 21, 2013 at 08:10 PM · c#delete

Deleteing a Spawned Object

I have been working on a mounting code and it works great now the only issue that is left is the fact that every time I mount I create a new mount and the old one is never deleted. How do I am really new to this spawning Idea and don't understand the code fully yet could some one help me with how to delete it in my if(isMounted == true) area here is the code.

 using UnityEngine;
 using System.Collections;
 
 public class GUIandSpawn : MonoBehaviour 
 {
     private string button1Text;
     public GUISkin Location1Button;
     
     public Transform thisChar;
     public Transform hoverBoard;
     public GameObject prefabToSpawn;
     public Transform spawn;
     public bool isMounted = false;
     public int dist = 1;
     
     
     void OnGUI()
     {
     GUI.skin = Location1Button;
         
         
         
     if(isMounted == true)
             
         {
             if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
                 {
                     thisChar.GetComponent<ThirdPersonController>().enabled = true;
                     thisChar.GetComponent<ThirdPersonCamera>().enabled = true;
                     thisChar.transform.parent = null;
                     isMounted = false;
                 }
             
         }
     if(isMounted == false)
         {
             if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
                 {
                     Vector3 v3Pos = thisChar.position - transform.up * dist;
                     GameObject clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;
                     thisChar.transform.parent = clone.transform;
                     thisChar.GetComponent<ThirdPersonController>().enabled = false;
                     thisChar.GetComponent<ThirdPersonCamera>().enabled = false;
                     clone.GetComponent<HoverBoards>().enabled = true;
                     isMounted = true;
                 }
         }
         
     }
 }
 
Comment
Add comment · Show 5
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 ExTheSea · May 21, 2013 at 08:15 PM 1
Share

Well i don't quite understand your question and why you made your code so that you get models just "lying around" but to answer your question. You can delete an object using

Destroy(gameobjectvariable);

avatar image robertbu · May 21, 2013 at 08:19 PM 1
Share

If you already have a mount, why don't you just move it rather than Instantiate() a new one and delete the old one.

avatar image davidflynn2 · May 21, 2013 at 08:26 PM 0
Share

What I am trying to do is make it so that when you press the GUI button it spawns your mount under you, you can use it then press the same button or another button and get rid of it till you call it again by pressing the button. But I am not having much luck at this.

avatar image robertbu · May 21, 2013 at 08:36 PM 1
Share

You could just enable and disable the mount and not bother with Instantiate() and Destory(). But if you want to destroy it, you have to save a reference to it:

At the top of the file:

 private GameObject clone;

The Instantiate();

 clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;

And the Destroy:

 Destroy(clone);
avatar image davidflynn2 · May 21, 2013 at 09:04 PM 0
Share

Convert this to an answer and I will mark it as correct as it was very helpful and helped me get what I needed.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · May 21, 2013 at 09:04 PM

You should save reference to clone in your class, instead of making it a local object. Then to delete it simply call Destroy(clone). So your code can look like this:

 public class GUIandSpawn : MonoBehaviour 
 {
     private string button1Text;
     public GUISkin Location1Button;
  
     public Transform thisChar;
     public Transform hoverBoard;
     public GameObject prefabToSpawn;
     public Transform spawn;
     public bool isMounted = false;
     public int dist = 1;

     private GameObject clone = null;
  
  
     void OnGUI()
     {
     GUI.skin = Location1Button;
  
  
  
     if(isMounted == true)
  
        {
          if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
           {
               thisChar.GetComponent<ThirdPersonController>().enabled = true;
               thisChar.GetComponent<ThirdPersonCamera>().enabled = true;
               thisChar.transform.parent = null;
               isMounted = false;
               if (clone != null)
                   Destroy(clone);
               clone = null;
           }
  
        }
     if(isMounted == false)
        {
          if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
           {
               Vector3 v3Pos = thisChar.position - transform.up * dist;
               clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;
               thisChar.transform.parent = clone.transform;
               thisChar.GetComponent<ThirdPersonController>().enabled = false;
               thisChar.GetComponent<ThirdPersonCamera>().enabled = false;
               clone.GetComponent<HoverBoards>().enabled = true;
               isMounted = true;
           }
        }
  
     }
 }
Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fading To New Scene Problem? 1 Answer

I know C#, but what Unity methods should I learn? 2 Answers

UDP implementation in Unity, unable to send to two different machines 0 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