- Home /
I cant add scripts to my weapons,i can't add Scripts to my weapon
Every time I try to add a shooting script to my weapon I get an error that says. " Cant add script, the script must derive from MonoBehaiviour." However, in the script it says MonoBehaiviour. what do I do
Heres my script:
using UnityEngine; using System.Collections;
public class ShootDemo : MonoBehaviour { public Rigidbody projectile; public float speed = 20;
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
},I am trying to make a gun. I keep trying to add scripts to my weapon to make it shoot. Visual Studio says that there are no errors so when I drag it to make it a child of my gun it says "can't add Script behavior VisualContainerAsset, the script needs to derive from MonoBehavior." in the script, it says MonoBehavior in the beginning.
Heres my script.
using UnityEngine; using System.Collections;
public class ShootDemo : MonoBehaviour { public Rigidbody projectile; public float speed = 20;
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
Answer by andrew-lukasik · May 22, 2018 at 09:06 PM
Unity requires that every MonoBehaviour scripts name, here 'ShootDemo', must match with file name i.e.: 'ShootDemo.cs'. You will get this error otherwise.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163223im_/https://answers.unity.com/themes/thub/images/avi.jpg)