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
1
Question by Mango_418 · Dec 28, 2020 at 07:40 PM · gameobjectdestroyif

Destroy(gameObject) does not work

I am trying to make a rhythm game, this code is to destroy the arrows when clicking a specific button when passing a specific place but when clicking the button the arrow is not destroyed, this is only part of the code but I suppose that here it is the problem

 if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             if (adentro)
             {
                 GameObject.Find("Casilla").GetComponent<logicaCuadro>().puntaje++;
                 GameObject.Find("Casilla").GetComponent<logicaCuadro>().texto.text = "Score: " +
                     GameObject.Find("Casilla").GetComponent<logicaCuadro>().puntaje.ToString();
                 Destroy(gameObject);
             }
         }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VoidPhoenix96 · Dec 28, 2020 at 08:58 PM

One possibility is that the code never runs the part with the destroy function. You can test this with Debug.Log(); If in the console you see what your wrote in the Debug.Log(); when the button is pressed, that means that the code is running. If it is not running try rewriting it in a different way.

I hope this helps.

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 sacredgeometry · Dec 28, 2020 at 09:00 PM

Just a word of warning. From the look of your code its pretty apparent that you are going to have some serious problems making a rhythm game. So firstly and genuinely good luck with it.


Secondly Find is really expensive and you are not only calling it once but are calling it (and get component) three times to get the same object. If you really need to call it at all store the returned value in a variable (ideally one that is scoped so that you only need to find it once per runtime) i.e.


 public GameObject Casilla { get; set; }
 public logicaCuadro LogicaCuadro { Get; Set; }
 
 void Start()
 {    
     Casilla  = GameObject.Find("Casilla")
     LogicaCuadro = casilla?.GetComponent<logicaCuadro>();
 }
 


Thirdly. Which gameObject are you trying to destroy? The one this code is on? What is adentro and is it being set to true?


Fourthly. Never call destroy unless absolutely necessary. I would imagine you are using multiple instances of this item that you are destroying and instantiating them again and again. Just pool and reuse them. A rhythm game is very time sensitive managed languages aren't great for that but you are only making it worse by asking the GC to kick in over and over again.


Fifthly the casing convention for classes in C# is Pascal Casing. Components are Classes so naming them where each word starts with a capital letter will stop confusion.

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

173 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 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 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 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 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 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 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 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 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

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

Problem with timed code-execution (audioplay and object destruction) 1 Answer

Trying to make a simple inventory: 0 Answers

Why Destroy(gameobject) fail once in a while? 2 Answers

Issues with destroying a game object 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