Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MangoM3lon · Jan 26, 2020 at 03:28 AM · gameobjectdestroydestroy objectdestroygameobject

Destroying Assests Not Permitted

 public class birdScript : MonoBehaviour
 {
     private GameObject bird;
 
     // Update is called once per frame
     void Update()
     {
         if(gameObject.transform.position.y < -20)
         {
             Destroy(gameObject);
         }
     }
 
     public void hitCheck(float playerx, float playery)
     {
         if(playerx - 2 < gameObject.transform.position.x && playerx + 2 > gameObject.transform.position.x)
         {
             if (playery - 2 < gameObject.transform.position.y && playery + 2 > gameObject.transform.position.y)
             {
                 DestroyBird();
             }
         }
     }
     void DestroyBird()
     {
         Debug.Log("die");
         bird = gameObject;
         Destroy(bird);
     }
 }

So basically I have a birdPrefab and on that prefab I have a bird script. In the script I want to destroy the bird if hitcheck is called, but it says that it is not permitted. I have tried to look for this in other places but none of them had answers specific for destroying the gameObject.

Comment
Add comment · Show 1
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 sacredgeometry · Jan 26, 2020 at 03:45 PM 0
Share

I know this isn't what you asked but I would suggest looking up spawn-pooling.

Deallocating memory during your games running can cause dips in performance as the GC kicks in. So if you can avoid using calls that do any deallocation outside of lifecycle methods that happen at the loading intervals. It will do you a bunch of favours when you come to want to optimise later on.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by medinaline · Jan 26, 2020 at 05:25 AM

From my understanding, Unity shows that message when the code would delete the actual asset in the project, rather than destroying the instance of the gameobject. Do you get the same error when calling Destroy(gameObject) rather than the assigned private variable bird, like you do in Update()?

Comment
Add comment · Show 4 · 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 MangoM3lon · Jan 26, 2020 at 01:49 PM 0
Share

Yes, I get the same error, when i do Destroy(gameObject).

avatar image Bonfire-Boy MangoM3lon · Jan 27, 2020 at 05:57 PM 0
Share

When and how does hitCheck get called? It looks like you're calling it on the prefab, which means it ends up trying to destroy the prefab. medinaline is correct: you can't do that.

You need to call it on the scene object ins$$anonymous$$d, the thing you instantiated from the prefab.

avatar image MangoM3lon · Jan 26, 2020 at 02:06 PM 0
Share

@medinaline

avatar image medinaline · Jan 27, 2020 at 01:46 PM 0
Share

Sorry, I was away all day yesterday.

I think it may have to do with how the bird object is being instantiated. It's really just a guess at this point. Good luck!

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

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

Optimize huge number of Destroy() calls (cannot use object pooling) 3 Answers

Destorying gameobjects on a network 0 Answers

Can you destroy an object using GetInstanceID? 0 Answers

Why are my Game Objects being destroyed? 0 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