- Home /
Why won't my script load?
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! :)
Off-topic: You can delete Start() and Update() from script if you won't use them. You can re-add them whenever you want.
Answer by zach-r-d · Jun 27, 2015 at 11:48 PM
This is because the class in Bullet_Destroy.cs is called NewBehaviourScript instead of Bullet_Destroy. The class name and the file name must always match.
Oh, How could I not have noticed that?! I guess I'm just used to the top part of the script being correct. I think because I renamed the script, ins$$anonymous$$d of na$$anonymous$$g it when I created it.
Thank you so much for your help :)