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 Blahman919 · Jun 04, 2014 at 01:02 PM · c#error

Missing Reference FPS help

Alright so I'm working on making my first FPS, first as in I started 2 days ago. I'm just working on coding mostly. And I've run into this problem: 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. Shooting.Update () (at Assets/Scripts/Shooting.cs:29)

Here are my scripts

 using UnityEngine;
 using System.Collections;
 
 public class Shooting : MonoBehaviour {
     public GameObject zero;
     public GameObject one;
     float bulletImpulse = 20f;
     public GameObject firePos;
     float ranNum = 0;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         Debug.Log (ranNum);
         if( Input.GetButton("Fire1") ) {
             ranNum = Random.Range (-1.0f, 1.0f);
             if (ranNum >=0 && ranNum <=1)
             {
             GameObject bullet2 = (GameObject)Instantiate(zero, firePos.transform.position + firePos.transform.forward, firePos.transform.rotation);
             bullet2.rigidbody.AddForce( firePos.transform.forward * bulletImpulse, ForceMode.Impulse);
             }
             else
             {
                 GameObject bullet1 = (GameObject)Instantiate(one, firePos.transform.position + firePos.transform.forward, firePos.transform.rotation);
                  bullet1.rigidbody.AddForce( firePos.transform.forward * bulletImpulse, ForceMode.Impulse);
             }
         }
 
     }
 }

And

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour {
     public static bool hit = false;
     float lifeTime = 6f;
     float hitTime = 1f;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
                 lifeTime -= Time.deltaTime;
                 
                 if (lifeTime <= 0)
                         Dead ();
                 if (hit) {
                         hitTime --;
 
                         if (hitTime <= 0) {
                                 hit = false;
                                 hitTime = 1f;
                         }
         
                 }
         }
     bool OnCollisionEnter (Collision collision){
         if (collision.gameObject.tag == "Enemy") {
                         hit = true;
 
         
                 } else
                     Destroy(gameObject);
         return hit;
     }
 
     void Dead(){
         Destroy (gameObject);
     }
 }
 

I'm trying to figure out why, when the bullets first start to be destroyed because of the lifeTime running out, they stop shooting. I'm using 2 prefabs for the 2 different bullets that shoot. Both of them give this error. Any ideas? Thanks for your time.

Edit

It seems that, no matter what, when the Destory(gameObject); is called, Unity can no longer use the prefab. Anyone have any idea why that may be?

Comment
Add comment · Show 12
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 AlucardJay · Jun 04, 2014 at 01:08 PM 0
Share

Chances are your bullets are colliding with something that is not tagged enemy at the instantiated position. You can overcome this collision with Physics.IgnoreCollision

avatar image Blahman919 · Jun 04, 2014 at 05:27 PM 0
Share

I tried that and it didn't seem to fix anything. After the 6 or so seconds, when the bullets start to be deleted I can no longer shoot.

avatar image robertbu · Jun 05, 2014 at 02:41 AM 0
Share

Are 'zero' and 'one' in the scene when you start the game? If so they will be destroyed and therefore you cannot clone (Instantiate()) them. If this is the problem, the solution is to make them a true prefab, not a scene object.

avatar image Hyperion · Jun 05, 2014 at 03:01 AM 0
Share

Why do you have

  if (collision.gameObject.tag == "Enemy") {
 hit = true;
  
  
 } else
 Destroy(gameObject);

This says that upon collision, say hit =true, BUT if it's NOT collided, destroy the bullet. @robertbu @Blahman919

avatar image Blahman919 · Jun 05, 2014 at 11:36 AM 0
Share

Alright so, I know for a fact it's the Destroy(gameObject) messing it up. @robertbu Yes, it's a prefab I made, I'm not sure what you mean by true prefab, they are objects in my assessts not in the scene itself. @Hyperion I'm not sure how it got like that, I changed it so that the Destroy(gameObject) is inside the if, not sure why I did it like that.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Krizzen · Jun 05, 2014 at 11:42 PM

The problem is in the Bullet class you have:

public static bool hit = false;.

Try this instead:

public bool hit;

A static variable is basically a single copy of that variable shared between all classes of that type. This means that if you have a few bullets, when one hits and does hit = true, then hit is true for all Bullets. So this means any bullet fired after the first hits and after lifeTime seconds, it thinks it has already hit and will be immediately destroyed.

A better solution is to use the handy built-in Destroy(GameObject gameObject, float time) method.

Change your code to this:

 using UnityEngine;
 using System.Collections;
  
 public class Bullet : MonoBehaviour {
     float lifeTime = 6f;
     float hitTime = 1f;

     // Use this for initialization
     void Start () {
         Destroy(gameObject, lifeTime);
     }
  
     // Update is called once per frame
     void Update () {
 
     }

     void OnCollisionEnter (Collision collision) {
         if (collision.gameObject.tag == "Enemy")
             Destroy(gameObject, hitTime);
     }
 }
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 jtok4j · Sep 21, 2017 at 08:17 PM -1
Share

Props for the great reply!

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

27 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

Related Questions

Getting index of the smallest number in a list 1 Answer

Particle circle HELP 1 Answer

Null in GetValidMethodInfo 0 Answers

error CS0122: `GameController.AddScore(int)' is inaccessible due to its protection level 1 Answer

error on getint 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