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 fossi · Aug 13, 2014 at 08:15 AM · gameobjectdestroytag

How do I make Destroy(gameObject) work in my script?

My code is a little long for me to post the entirety of it, but I was able to recreate the issue I'm having with a much trimmed down script, which is shown below. I'm not including what I did to create the mesh since I feel it is irrelevant to my question, and because the script is trimmed down from my original code, it might seem overly complicated for what I'm trying to do. However, the problem still remains so please bear with me.

 using UnityEngine;
 using System.Collections;
 
 public class CoreController : MonoBehaviour {
 
    private RaycastHit2D[] leftHits;
    private bool moveLeft;
 
    public void createMesh() {
       //create mesh
    }
 
    void Start () {
       moveLeft = false;
       tag = "step1";
       createMesh();
    }
     
    void Update () {
       leftHits = Physics2D.RaycastAll(new Vector2(transform.position.x-0.1f, transform.position.y+1.5f/2), -Vector2.right);
         
       if(Input.GetKeyDown("left")) {
          moveLeft = true;
          tag = "step2";
       }
 
       if(!GameObject.Find("step1")) {
          if(moveLeft) {
             if(leftHits.Length > 0) {
                if(tag == "step2" && leftHits[0].collider.gameObject.tag == "step2") {
                   Destroy(gameObject);
                }
             }
          }
  
          //Part A
          if(tag == "step2") {
             tag = "step3";
          }
       }
    }
 }

This script is attached to a gameObject that is instantiated by another script to many random locations on the x-y plane. Without Part A, if a gameObject detects another gameObject to the left of it with the same tag "step2", the gameObject gets successfully destroyed when I click the left button. Now, I want the remaining gameObjects to have the tag "step3". However, with Part A included, the Destroy(gameObject) line is skipped and every gameObject remains and attains the new tag "step3".

It seems that when I include Part A, the if condition

 (tag == "step2" && leftHits[0].collider.gameObject.tag == "step2")

is never true for any of instantiated gameObjects because Part A gets executed first. But why?

Any help would be much appreciated.

Note: I tried FatWednesday's suggestion of putting "return;" right after the Destroy(gameObject) line, but I'm still getting the same problem.

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 kebin · Aug 13, 2014 at 08:43 AM 0
Share

can you show us your entire script?

avatar image Ekta-Mehta-D · Aug 13, 2014 at 08:53 AM 0
Share

Please post your entire code

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FatWednesday · Aug 13, 2014 at 08:23 AM

Destroy is not an immediate call, never encountered this issue myself but its possible that something is causing it to be removed from the list of objects to destroy before that list is processed.

In any case, to avoid any of the rest of that method running, you could simply putting a "return;" line after the destroy call, Or if there are other parts of the method you still want to run then I'd suggest setting a flag for something like "hasBeenMarkedForDestroy" and then check that flag before doing any other parts of the method that you want to exclude if the destroy got called.

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

5 People are following this question.

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

GameObject.FindWithTag doesn't work! 1 Answer

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

how do i create a game object array with gameobjects of multiple tags 1 Answer

Destroying Multiple Objects with Tags 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