Raycast Shooting Error

I tried to made this shooting script:

    void Shoot() {
       
        //Play shoot animation
        if (!anim.GetBool ("isAiming")) {
            anim.Play ("Fire");
        } else {
            anim.SetTrigger("Shoot");
        }
       
        //Remove 1 bullet
        currentAmmo -= 1;
       
        //Play shoot sound
        AudioClips.mainAudioSource.clip = AudioClips.shootSound;
        AudioClips.mainAudioSource.Play();
       
        //Start casing instantiate
        if (!ReloadSettings.casingOnReload) {
            StartCoroutine (CasingDelay ());
        }
       
        //Show the muzzleflash
        StartCoroutine (MuzzleFlash ());
       
        //Raycast bullet
        RaycastHit hit;
        Ray ray = new Ray (transform.position, transform.forward);
       
        //Send out the raycast from the "bulletSpawnPoint" position
        if (Physics.Raycast (Spawnpoints.bulletSpawnPoint.transform.position,
                             Spawnpoints.bulletSpawnPoint.transform.forward, out hit, ShootSettings.bulletDistance)) {
           
            //If a rigibody is hit, add bullet force to it
            if (hit.rigidbody != null)
                hit.rigidbody.AddForce (ray.direction * ShootSettings.bulletForce);
           
            //********** USED IN THE DEMO SCENES **********
            //If the raycast hit the tag "Target"
            if (hit.transform.tag == "Target") {
                //Spawn bullet impact on surface
                Instantiate (Prefabs.metalImpactPrefab, hit.point,
                             Quaternion.FromToRotation (Vector3.forward, hit.normal));
                //Toggle the isHit bool on the target object
                hit.transform.gameObject.GetComponent<TargetScript>().isHit = true;
            }
           
            //********** USED IN THE DEMO SCENES **********
            //If the raycast hit the tag "ExplosiveBarrel"
            if (hit.transform.tag == "ExplosiveBarrel") {
                //Toggle the explode bool on the explosive barrel object
                hit.transform.gameObject.GetComponent<ExplosiveBarrelScript>().explode = true;
                //Spawn metal impact on surface of the barrel
                Instantiate (Prefabs.metalImpactPrefab, hit.point,
                             Quaternion.FromToRotation (Vector3.forward, hit.normal));
            }
        }
    }

In a raycast shooting script:

    private Camera raycastcamera;

    //Shoot
    void Shoot() {
       
        //Play shoot animation
        if (!anim.GetBool ("isAiming")) {
            anim.Play ("Fire");
        } else {
            anim.SetTrigger("Shoot");
        }
       
        //Remove 1 bullet
        currentAmmo -= 1;
       
        //Play shoot sound
        AudioClips.mainAudioSource.clip = AudioClips.shootSound;
        AudioClips.mainAudioSource.Play();
       
        //Start casing instantiate
        if (!ReloadSettings.casingOnReload) {
            StartCoroutine (CasingDelay ());
        }
       
        //Show the muzzleflash
        StartCoroutine (MuzzleFlash ());
       
        //Raycast bullet
        RaycastHit hit;
        Ray ray = new Ray (transform.position, transform.forward);

        Vector3 rayOrigin = raycastcamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));

        //Send out the raycast from the "bulletSpawnPoint" position
        if (Physics.Raycast (rayOrigin, raycastcamera.transform.forward, out hit, ShootSettings.bulletDistance)) {
           
            //If a rigibody is hit, add bullet force to it
            if (hit.rigidbody != null)
                hit.rigidbody.AddForce (-hit.normal * ShootSettings.bulletForce);
           
            //********** USED IN THE DEMO SCENES **********
            //If the raycast hit the tag "Target"
            if (hit.transform.tag == "Target") {
                //Spawn bullet impact on surface
                Instantiate (Prefabs.metalImpactPrefab, hit.point,
                             Quaternion.FromToRotation (Vector3.forward, hit.normal));
                //Toggle the isHit bool on the target object
                hit.transform.gameObject.GetComponent<TargetScript>().isHit = true;
            }
           
            //********** USED IN THE DEMO SCENES **********
            //If the raycast hit the tag "ExplosiveBarrel"
            if (hit.transform.tag == "ExplosiveBarrel") {
                //Toggle the explode bool on the explosive barrel object
                hit.transform.gameObject.GetComponent<ExplosiveBarrelScript>().explode = true;
                //Spawn metal impact on surface of the barrel
                Instantiate (Prefabs.metalImpactPrefab, hit.point,
                             Quaternion.FromToRotation (Vector3.forward, hit.normal));
            }
        }
    }

But i get this error:

Line 534:

Vector3 rayOrigin = raycastcamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));

Hi,

I don’t see you actually assigning the “raycastcamera” variable.
It’s a private variable, so you should be getting it somewhere, like in Start() or Awake();

raycastcamera = Camera.main;
1 Like

oh, lol, i forget that :)), Thanks, i put that:

raycastcamera = GetComponentInParent<Camera>();

Thanks again!