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 wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 21, 2018 at 02:49 PM · instantiateunity 2drespawndestroygameobject

Respawn Management

Good Afternoon :) Currently, I'm making a simple 2D game In order to respawn enemies, I've made a respawn manager. It works perfectly when I run it. However, after few seconds, it just stops working alt text alt text As you can see, there's enemy prefab at the first pictiure. But it suddenly disappears after few second (Pic2)

And this kind of error message comes out

"MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it"

How should I solve this problem?? Please read the comments, too. Thank You :)

1.png (22.0 kB)
2.png (21.7 kB)
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 wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 21, 2018 at 03:01 PM 0
Share

These might be the scripts that have problems

alt text

alt text

3.png (83.9 kB)
4.png (77.2 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Jun 21, 2018 at 03:16 PM

This is very strange, you saying the variable Obj gets null for nor reason?

Is Enemy a prefab? are you sure you assigned it by draggin from some of your assets folder?

If you drag a prefab from some assets folder, and there is no code assigning new object in variable Obj, this can only be caused because the asset has changed name, or deleted or something....


That void Destroy in the second script, where is it palced? at what object? If you want to destroy the object containig that script, the instantiated object in the scene you need to do directly:

 Destroy (gameObject);

gameObject (with minusc "g" refears to the GameObject containing the script). if you write "this" i think is refearing to class so it destroys the prefab.

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 LeeroyLin · Jun 21, 2018 at 03:20 PM

I guess that in your RespawnManager script, you destroy the enemy in few seconds. I'm not sure, but I guess you destroy the Prefab gameObject.

 public GameObject obj;
 
 Destroy(obj);

This will cause your problem to happen. So, you can destroy the instance instead of the prefab.

 public GameObject obj;
 GameObject enemy1 = Instantiate(obj);
 Destroy(enemy1 );

If you need every enemy to destroy in a few seconds, you can create a script to delay destroy the object which the script attached to, and then drag this script to your enemy prefab.

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 tormentoarmagedoom · Jun 21, 2018 at 03:31 PM 0
Share

Good day Leeroy,

Tis can work if the same script that instantiates the object, is who destroys it. But if the object needs to destroy itself, he doesnt know that "enemy1" means.

:D

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

108 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

Related Questions

Respawn function after character is destroyed 3 Answers

Help with error when using instantiate to respawn an enemy. 0 Answers

Can't instantiate "scene objects" [Unity Mirror Multiplayer] 0 Answers

Adjacent Placement of Objects according to varying height 0 Answers

How to instantiate and destroy objects with 2d trigger 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