I know similar topic were made I and follow them step by step I still I miss something. I’m putting my first step in Unity, I’ve watch official tutorial where guy is making rockets (Instantiate - Unity Learn) I follow it and I have an issue. I set lifetime of my bullet on 1,5 sec. First bullet is destroyed and then I can not shot anymore. There is my script attached to the gun:
public class bullet : MonoBehaviour {
public Rigidbody bulletPrefab;
public Transform barellEnd;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody bulletInstance;
bulletInstance = Instantiate(bulletPrefab, barellEnd.position, barellEnd.rotation) as Rigidbody;
bulletInstance.AddForce(barellEnd.forward * 5000f);
}
}
}
And another attached to my bullet:
public class shootScript : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Target"))
{
Destroy(other.gameObject);
}
}
}
And
using UnityEngine;
using System.Collections;
public class destroyBullet : MonoBehaviour {
// Use this for initialization
void Start () {
float lifetime = 1.5f;
Destroy(gameObject, lifetime);
}
}
What I’m doing wrong? I have on my console Error:
MissingReferenceException: The object of type ‘Rigidbody’ 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.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:54)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:144)
bullet.Update () (at Assets/Skrypty/bullet.cs:14)