- Home /
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?
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
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.
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.
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
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.
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);
}
}
Your answer
Follow this Question
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