missing reference exception


I am working on a shooting game. The issue is when the enemy is higher than the player or the other way around he always misses him.

So I used the Function MoveBullets to make sure the direction and angle is always correct.

but it gives me an error that: Missing reference exception.

because the bullet gets destroyed after it hits the player or a certain amount of time.

Any suggestions. Thanks


var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 1.0;
var Player:Transform;
private var lastShot = -10.0;
    var enemyshoutsound : AudioClip;

    function fire(){

    // Did the time exceed the reload time?
    if (Time.time > reloadTime + lastShot+1) {

        // create a new projectile, use the same position and rotation as the Launcher.
        var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
            PlayAudioenemyshoutsound(enemyshoutsound, transform.position, 1);
        // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
//instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
        //instantiatedProjectile.velocity = transform.TransformDirection(Player.position);


         moveBullet(instantiatedProjectile, instantiatedProjectile.position, Player.position, 0.2);

        // Ignore collisions between the missile and the character controller
        Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

        lastShot = Time.time;


function PlayAudioenemyshoutsound (clip : AudioClip, position : Vector3,volume : float  ) {
    var go = new GameObject ("One shot audio");
    go.transform.position = position;
    var source : AudioSource = go.AddComponent (AudioSource);
    source.clip = clip;
    source.volume = volume;
    source.Play ();
    Destroy (go, clip.length);
    return source;

function moveBullet (instantiatedProjectile, startPos : Vector3, endPos : Vector3, time : float) {
while(instantiatedProjectile !=null){
    var i = 0.0;
    //var rate = 1.0/time;
    var rate = 5;
    while (i < 1.0) {
        i += Time.deltaTime * rate;
        instantiatedProjectile.position = Vector3.Lerp(startPos, endPos, i);

Save the bullet to a prefab, and use that, not the object in the scene.

By the way, your code is Very long. for one thing, your whole function `PlayAudioenemyshoutsound` can be replaced with `audio.PlayClipAtPoint(clip, position)`, and it will do just that. However, +1 for figuring out how to do this the hard way!!! :)