• 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
Question by skittlesloli · Jul 29, 2014 at 03:59 AM · instantiateprefabobjectdestroy

How to delete an instantiated object

So it seems this is a problem people have encountered before, but none of the code looks like what my Instructor has shown us, and I'm having trouble deleting my object.

What happens is, you push a button and it instantiates a colored cube to the player. What I'm trying to make happen, is when the player has a cube and clicks on a matching cube on a door, it disappears and the cube on the door changes color to indicate it's 'unlocked'.

So the first bit is how we get the key when we push the button.

 var KeyPreFab: GameObject;
 var ThePlayer: GameObject;
 var TheKey: GameObject;
 
 function Start () {
 }
 
 function Update () {
 }
 
 function OnMouseUpAsButton () {
     Debug.Log("Button has been pushed.");
     renderer.material.color = Color.green;
 
     //turn yellow key checkbox on
     if (ThePlayer.GetComponent(PlayerInventory).YellowKey == false) {
         TheKey = Instantiate (KeyPreFab, ThePlayer.transform.position, Quaternion.identity);
         TheKey.transform.parent = ThePlayer.transform;
         TheKey.transform.localPosition.y += 0.5;
         TheKey.transform.localPosition.z += 0.5;
         }

And I imagine that in my script for the door's 'keyhole' it will start off something like

 if (ThePlayer.GetComponent(PlayerInventory).YellowKey == true) {
                 } 

but I honestly don't know how to approach this after that. I've tried rendering the key false, I've tried destroying it... The 'keyhole' doesn't even change color for me, and the code so far is exactly the same code I have above (but with a different message and color render).

I hope I've given enough information to be useful. I thought this would be an easy thing to do for my assignment, but I guess not :P.

Comment

People who like this

0 Show 4
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 VesuvianPrime · Jul 29, 2014 at 10:24 AM 0
Share

Hi Skittlesloli

Have you tried throwing in some Debug.Log("Button pressed!") calls just to make sure your keyhole is working?

avatar image skittlesloli · Jul 29, 2014 at 01:30 PM 0
Share

Yes, I have the "button pressed" changed to "you have used the key". I have also set the keyhole to turn black, and that's not happening either. Edit: Thank you BoredMormon, for the edit. I wasn't sure how to fix that.

avatar image VesuvianPrime · Jul 30, 2014 at 12:33 AM 0
Share

Perhaps you could post some of that code as well? I think that's the most important thing to see for us to help you.

avatar image skittlesloli · Jul 30, 2014 at 03:25 AM 0
Share

Sure :P

 var KeyPreFab: GameObject;
 var ThePlayer: GameObject;
 var TheKey: GameObject;
  
 function Start () {
 }
  
 function Update () {
 }
  
 function OnMouseUpAsButton () {
     Debug.Log("You have used the key.");
     renderer.material.color = Color.black;
  
     //turn yellow key checkbox on
     if (ThePlayer.GetComponent(PlayerInventory).YellowKey == true) {
         //this is where I get lost
         }
 

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to destroy instantiated objects. 1 Answer

Cannot destroy child object in prefab- Error 1 Answer

help for start my project... 2 Answers

Destroying Objects. 1 Answer

How to Reference Player when Instantiating Prefab 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