I am getting an error in this script?
UnityEngine does not contain a definition for rigid body (Lines: 22,24)
public class GunShoot : MonoBehaviour
{
public GameObject BulletPrefab;
public float BulletSpeed;
public int BulletsInClip;
public AudioClip GunshotSound;
void Update () {
if (Input.GetButtonDown("Shoot")){
Shoot();
}
}
void Shoot() {
var bullet = Instantiate(BulletPrefab, transform.Find("BulletSpawn").position, transform.Find("BulletSpawn").rotation);
bullet.rigidbody.AddForce(transform.forward * BulletSpeed);
audio.PlayOneShot(GunshotSound);
BulletsInClip--;
}
}
In Unity you need to grab the rigidbody like this
bullet.GetComponent<Rigidbody >().AddForce(...)
-- this is c# btw I'm not sure how different it is in JavaScript.A
var
in this case means that the created instance is of typeUnityEngine.Object
. You need to specify a type cast explicitly:or
In general it is preferrable to use an explicit type as it increases readability (my oppinion) like:
Use GetComponent to get the RigidBody something like this.