Hi,
I am trying to make a FPS game, and so far I have my player moving around and shooting. I am trying to make it so that when the bullets hit something, they destroy themselves.
This is the code i am using to create the bullets:
using UnityEngine;
using System.Collections;
public class FP_Shooting : MonoBehaviour {
public GameObject Bullet_prefab;
float bulletImpulse = 50f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
Camera cam = Camera.main;
GameObject thebullet = (GameObject)Instantiate(Bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
thebullet.GetComponent<Rigidbody>().AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
Debug.Log ("Click");
}
}
}
And this is the code I am using to destroy them on contact:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter() {
Destroy (gameObject);
}
}
In the Inspector, under my Bullet_Destroy script, it says “The associated script can not be loaded. Please fix any compile errors and assign a valid script”. I have no idea why it is saying this, ans I can’t see any errors in the script.
Any help would be greatly appreciated!