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 Apocolyptosaur · Aug 12, 2012 at 07:04 PM · instantiatedestroylistscloneweapons

How can I destroy my Instance without renaming it?

In my game, I instantiate weapons when I equip them. I have a list of transforms as my list of weapons, and my selected weapon is also a transform. when I press the key I need to press in order to equip a weapon, it instantiates the weapon into my players hand. The problem I have is with destroying the equipped weapon when I want a different weapon. Every time I press the "switchWeapon" key, it cycles through my list and gives me the next weapon in the list.

How the h**l do I upload code on here?

My problem is specifically with those last couple lines ^^ I'm getting an "Object reference not set to an instance of an object"

Any help?

Comment
Add comment · Show 2
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 Loius · Aug 12, 2012 at 07:48 PM 1
Share

You copy-paste the code and then use the square "101-010" button to format it so our eyes don't bleed trying to read it. ^_^

avatar image Muuskii · Aug 12, 2012 at 08:21 PM 0
Share

I would like to note that the method you're using may not be the most effective; since you're instantiating and destroying things when you know how many of them exist.

$$anonymous$$eeping the GameObjects in an array and hiding them when not in use may actually be a more efficient way to do things. It would be easier on your memory management too, leaving more performance for amazing graphics/physics.

2 Replies

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

Answer by Meltdown · Aug 12, 2012 at 08:24 PM

Each weapon should be a prefab in your project.

When you instantiate it, the code should look something like this...

 [SerializeField] GameObject _myWeaponPrefab;
 
 GameObject weapon = GameObject.Instantiate(_myWeaponPrefab, player.transform.position, Quaternion.identity);

You now have a reference to the instantiated weapon, which you can destroy by simply calling

 Destroy(weapon);
Comment
Add comment · Show 2 · 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 Apocolyptosaur · Aug 12, 2012 at 09:29 PM 0
Share

So I integrated this into my current code, but it's still not working. There's no errors, but the sword isn't being destroyed, either. I know the function is being called though, because I put a debug statement into it and the Debug is co$$anonymous$$g through.

avatar image Meltdown · Aug 13, 2012 at 06:27 AM 0
Share

Did you get it working?

avatar image
0

Answer by Budoray · Aug 14, 2012 at 05:53 AM

Greg, you solved my issue, but I can't vote for your answer. My issue was that I had a prefab declared as a Transform. I cast it from the Instantiate method as a GameObject with no errors or warnings. Called Destroy with no errors or warnings. However, the prefab would remain on the screen. Declaring my prefab as a GameObject fixed that issue. Many thanks.

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

11 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

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

How to delete instantiated GameObject 4 Answers

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

Destroy a specific instantiated clone? 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