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 2Tie · Jan 17, 2011 at 04:28 AM · instantiatedestroyclone

Deleting an instance

I am wondering how to delete an instantiate()'d object from inside code. For example, in one object's code, i have

if (Input.GetButtonDown("Shield"))
{
    if (shielding == 0)
    {
        Instantiate(shieldobject,transform.position, transform.rotation);
        shielding = 1;
    }
} else {
    if (shielding == 1)
    {
    shielding = 0;
    }
}

And in my Shield prefab is a code file that goes like this:

function Update () {
    if(Input.GetButtonDown("Shield"))
    {
        Debug.Log("destroyed");
        Destroy(gameObject)
    }
}

But the shield clone doesn't delete itself. How can i have it delete itself?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fireDude67 · Jan 17, 2011 at 04:33 AM

When you Instantiate() an object, you can store it using something like this:

var newObject = Instantiate(shieldobject,transform.position, transform.rotation);

Then later when you want to destroy it, Destroy(newObject)

Comment
Add comment · Show 5 · 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 2Tie · Jan 17, 2011 at 04:45 AM 0
Share

But with my code as if (Input.GetButtonDown("Shield")) { if (shielding == 0) { var tempshield = Instantiate(shieldobject,transform.position, transform.rotation); shielding = 1; } } else { if (shielding == 1) { shielding = 0; Destroy(tempshield); } } , the shield instance still appears in the Hierarchy as Shield(Clone)...

avatar image fireDude67 · Jan 17, 2011 at 05:24 AM 0
Share

That is what is supposed to happen

avatar image 2Tie · Jan 17, 2011 at 11:11 PM 0
Share

the shield(clone) object isn't destroyed, though.

avatar image fireDude67 · Jan 18, 2011 at 05:27 AM 0
Share

Do you get any errors?

avatar image · Jan 18, 2011 at 05:34 AM 0
Share

In your code, 'var tempshield' only exists inside the 'shielding == 0' brace. Declare it outside of there and it should work.

avatar image
0

Answer by _Petroz · Jan 18, 2011 at 07:33 AM

Alternatively you could control the destruction from the parent class like this:

var shield : GameObject;

function Update() { if (Input.GetButtonDown("Shield")) { if (!shield) { shield = (GameObject)Instantiate(shieldobject,transform.position, transform.rotation); } } else { if (shield) { Destroy(shield); } }

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 · Jan 18, 2011 at 07:48 AM 1
Share

GetButtonDown only returns true for the frame that the button is first pressed. Your other suggestion duplicates the first answer + comments.

avatar image _Petroz · Jan 18, 2011 at 09:42 AM 0
Share

You are right about the GetButtonDown, I removed that part of my post. Although the other person suggested it, $$anonymous$$e is a clearer answer which is the goal of Answers. There is no harm in improving someone else's answer. The goal is to have the best answer not the most original. I also replaced his int with a pointer which is clearer as well.

avatar image 2Tie · Jan 18, 2011 at 11:31 PM 0
Share

The compiler says i need a semicolon after the (GameObject) right before the instantiate.

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

No one has followed this question yet.

Related Questions

Pass a copy of a GameObject as variable to another script? 1 Answer

How to delete instantiated GameObject 4 Answers

Destroy a specific instantiated clone? 2 Answers

How can I destroy my Instance without renaming it? 2 Answers

Instantiate and Destroy an object while crossing a line with its clone also 1 Answer


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