• 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 BKarner · Mar 10, 2014 at 04:35 PM · c#gameobjectprefabspawning

Destroying Game object (Not destroying) C#

I currently have a script which spawns objects at the top of the screen and fall with gravity. However, when I call "Destroy(prefab)", the prefab does not destroy at all. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class AsteroidManager : MonoBehaviour {
     
     public float minTime; 
     public float maxTime;
     public bool doSpawn = true;
     
     public GameObject prefab;
     
     public Vector3 minSize, maxSize;
     public float minX, maxX;
     
     public Vector3 startPosition;
     
     // Use this for initialization
     void Start () {
         rigidbody.useGravity = false;
         doSpawn = false;
         renderer.enabled = true;
         enabled = false;
         GameController.GameStart += GameStart;
         GameController.GameOver += GameOver;
     }
     
     // Update is called once per frame
     void Update () {
         if(prefab.transform.localPosition.y < -5){
             Destroy(prefab);    
         }
     }
 
     IEnumerator Spawner(){
         while(doSpawn) {
             Vector3 v = new Vector3(Random.Range(minX, maxX), 25, 0);
             Quaternion r = new Quaternion(0,0,0,0);
                Instantiate(prefab, v, r);
             prefab.renderer.enabled = true;
             prefab.rigidbody.useGravity = true;
                yield return new WaitForSeconds(Random.Range(minTime, maxTime));
         }
     }
     
     private void GameStart () {
         Vector3 temp;
         temp.x = 0; temp.y = 0; temp.z = 0;
         rigidbody.velocity = temp;
         rigidbody.useGravity = true;
         transform.localPosition = startPosition;
         renderer.enabled = true;
         enabled = true;
         doSpawn = true;
         StartCoroutine(Spawner());
     }
     private void GameOver () {
         rigidbody.useGravity = true;
         Destroy(prefab);
         
         renderer.enabled = false;
         doSpawn = false;
         enabled = false;
     }
 }

Any help would be greatly appreciated.

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

1 Reply

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

Answer by robertbu · Mar 10, 2014 at 04:38 PM

You are trying to destroy the prefab...the blueprint for all of your asteroid. Luckily, since the prefab is not moving, the condition is never met. These lines of code...

 void Update () {
    if(transform.position.y < -5){
      Destroy(prefab); 
    }
 }

...need to be on a script attached to the asteroid prefab. That is, each asteroid is needs to be responsible for destroying itself. The alternative is to keep a generic list of asteroids created in your AsteroidManager and then check each one each frame, destroying it and removing it from the list if the condition is met.

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 BKarner · Mar 10, 2014 at 04:51 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class AsterBeh : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(transform.localPosition.y < -5){
             Destroy(this.gameObject);    
         }
     }
 }

Like this?

Got it working. Thank you very much for your help Robertbu. You've been an amazing help.

avatar image robertbu · Mar 10, 2014 at 05:59 PM 0
Share

Note unless you are doing parenting, line 13 should use transform.position, not transform.localPosition. I know my original answer used 'localPosition'.

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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Object is Instantiating on same position 2 Answers

Thrown objects always have same rotation 2 Answers

How to make a gameObject instantiate a prefab for every 250 damage taken ? 4 Answers

My Health Bar Script Won't Work When I Turn My Enemy Into A Prefab 2 Answers

Multiple Cars not working 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