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 HellGateWizard · Jan 02, 2021 at 08:21 AM · instantiatedestroyprojectile

Getting an error message when destroying a instantiated prefab

I've been taking the create with code course and I am stuck on 7th step on lesson 2.2. Here is a link to the lesson I'm taking.

https://learn.unity.com/tutorial/lesson-2-2-launch-projectiles-by-instantiating-prefabs?uv=2018.4&courseId=5cf96c41edbc2a2ca6e8810f≺ojectId=5cdcc312edbc2a24a41671e6#5ce3479eedbc2a274fe5904f

Carl instructs us to create a new script and attach it to the projectile with this code written inside.

 public class DestroyOutOfBounds : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (transform.position.z > 50) {
 
             Destroy(gameObject);
 
         }
     }
 }


Now what it's supposed to do is destroy the projectile after it instantiates and goes out 50 units on the Z-Axis. However, when I test it out, the spacebar becomes irresponsive and I get an error message down below that says

"MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. PlayerController.Update () (at Assets/Scripts/PlayerController.cs:37)"

Has Unity's code changed since Carl did this video? because I can't for the life of me figure out why it's doing this, it feels like the Unity Engine is ignoring the conditions of the if statement and destroying it immediately!

Comment
Add comment · Show 4
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 Llama_w_2Ls · Jan 02, 2021 at 08:21 AM 0
Share

Is this script on the original prefab as well? (The object you are instantiating from). Is so, that will be destroyed and therefore you can't instantiate from it anymore, since it doesn't exist, and so you'll get an error when trying to instantiate it next time, in the PlayerController script. @HellGateWizard

avatar image prakyathd801 · Jan 02, 2021 at 12:04 PM 0
Share

Could you share the code which instantiates the Prefab?

avatar image unity_HTr7mOeTIRzvVA · Jan 02, 2021 at 02:47 PM 0
Share

Once you make the projectile as Prefab, make sure that no more projectile object resting in your hierarchy. Delete it or Deactivate it or $$anonymous$$ake sure you referenced the "Prefab projectile asset " on your gun instead of the object that is in the hierarchy

Look at your PlayerController script at line 37. What did you put there, or can you show us the code?

avatar image trannel · Jan 02, 2021 at 08:38 PM 0
Share

Can you post PlayerController? this is where the error comes from. also, the error is pretty clear, and you probably have to add a null check.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Revolution_Game · Jan 02, 2021 at 02:09 PM

I think you instantiate your object at > 50 in z-axis, so it gets destroyed once instantiated.

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 Llama_w_2Ls · Jan 02, 2021 at 02:42 PM 0
Share

That wouldn't necessarily create an error message. The desired effect is to destroy the clone in the first place. Unless, another script is trying to access anything from the clone.

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

142 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

Related Questions

Scripts not on instantiated GameObjects? 1 Answer

Firing only a single projectile at a time, then adding a second. 2 Answers

i want to destroy a projectile after it passes a limit on x axis after instantiation(2d game) 1 Answer

walk through an object once 0 Answers

Destroying childs and Instantiate [C#] 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