Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 OhYeahitsJosh · Jul 20, 2015 at 02:13 AM · destroyclone

Destroy instantiated object instead of prefab

Hello, I'm trying to write a script that gives the user two choices. The user either chooses left or right and then it repeats. However, I'm trying to do the following:

  1. Destroy both objects when the user either presses left or right

  2. Make sure that both choices are not the same (e.g. left and right choices cannot both be healthy food)

So I'm trying to work on the first item and I'm having a lot of trouble. I'm REALLY new to scripting and C# in general so I'm not sure how to destroy the cloned objects instead of the prefab. I had a function for destroying the objects but it just deleted the prefabs instead. After that, I tried having a separate script for destroying objects that would be applied to cloned objects but I'm not sure how to apply them to cloned objects. I could really use some help here on the first part (and if you're feeling generous the second part).

Here's my code:

 using UnityEngine;
 using System.Collections;
         
         public class randomFood : MonoBehaviour {
             
             public float randFloat;
             public GameObject healthyFood;
             public GameObject unhealthyFood;
             public GameObject notFood;
             public Transform emptyObject;
         
             void Start ()
             {
                 StartGame ();
             }
         
             void Update ()
             {
                 if (Input.GetKeyDown (KeyCode.LeftArrow)) {
                     print ("Left Choice");
                     StartGame ();
                 } else if (Input.GetKeyDown (KeyCode.RightArrow)) {
                     print ("Right Choice");
                     StartGame ();
                 }
             }
         
             void StartGame ()
             {
                 randFloat = Random.Range (0, 70);
                 if (randFloat <= 40) {
                     Instantiate (healthyFood, emptyObject.position, emptyObject.rotation);
                     Debug.Log ("Healthy Food");
                 }
                 {
                 if (randFloat > 40 && randFloat <= 60) {
                     Instantiate (unhealthyFood, emptyObject.position, emptyObject.rotation);
                     Debug.Log ("Unhealthy Food");
                 }
                 {
                 if (randFloat > 60 && randFloat <= 70) {
                     Instantiate (notFood, emptyObject.position, emptyObject.rotation);
                     Debug.Log ("Not Food");
                         }
                     }
                 }
             }
         }

And here's the destroy script:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyedFood : MonoBehaviour
 {
     
     void Start ()
     {
         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
             Destroy (gameObject);
         } else if (Input.GetKeyDown (KeyCode.RightArrow)) {
             Destroy (gameObject);        
         }        
     }
 }
 

Please help me out! Thanks! :)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Marc81 · Jul 20, 2015 at 02:55 AM

Just save the instance in a variable so you are able to destroy that instance later. And for now you can get rid of your DestroyedFood class, as you will not need that one :)

To destroy the instance, add this to your randomFood class:

 private GameObject _instance;

Then extend your Instantiate(... calls like this:

 _instance = Instantiate(

And before your StartGame (); calls in your Update method just destroy the instance:

 Destroy(_instance);
Comment
Add comment · Show 1 · 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 jtgvhbth · Aug 24, 2017 at 05:41 PM 1
Share

Despite this being 2 years old, it still works! Thanks a bunch man!

avatar image
1

Answer by ManguGames · Jul 22, 2019 at 09:40 AM

I know that this post is a bit old but others will go looking for an answer, just like I entered, and this is what happens:

When you are going to assign the GameObject to the public variable from the inspector, a small window opens, and inside it has two small tabs, named: ASSETS and SCENE, and you should keep in mind to select the prefabs from ASSET, and also keep in mind that you must do it from the object that is in the hierarchy and not from your assets folder where the script is. I attach a screen alt text

,I know that this post is a bit old but others will go looking for an answer, just like I entered, and this is what happens:

When you are going to assign the GameObject to the public variable from the inspector, a small window opens, and inside it has two small tabs, named: ASSETS and SCENE, and you should keep in mind to select the prefabs from ASSET, and also keep in mind that you must do it from the object that is in the hierarchy and not from your assets folder where the script is. I attach a print screen: alt text


annotation-2019-07-21-095707.jpg (274.8 kB)
annotation-2019-07-21-095707.jpg (274.8 kB)
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
avatar image
0

Answer by meat5000 · Jul 20, 2015 at 02:49 AM

You actually cant destroy prefabs unless you use DestroyImmediate.

I'm guessing you have linked your script referenced objects from objects that actually already exist in scene. This is not a prefab.

Drag these objects to some folder in the project manager.

Thats a prefab. Drag that blue thing back in to the placeholders in your scripts for your objects. Now you are spawning from a prefab.

You can :

 GameObject myGo;
 myGo = Instantiate (notFood, emptyObject.position, emptyObject.rotation);
 myGo.name = "NameDontMatterGonnaDieInAMinute";
 Destroy(myGo);//BANG
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying a Transform Component 1 Answer

Component not being accessed in ArrayList 1 Answer

How to destroy a clone (instantiate in scene) in the their own script 1 Answer

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

Destroy Prefab 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