Charlotte Barnekow
Charlotte Barnekow

Reputation: 77

C# Issues with setting muzzleflash inactive

I'm in C# and I want my handgun Muzzleflash to be inactive after each shot. I keep getting error message:

"Error CS8025 Feature 'local functions' is not available in C# 4. Please use language version 7 or greater."

WHEN I fix this I still get compiler errors and can't run game. It would be highly appreciated if you could take a look at code below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunFire : MonoBehaviour
{

    public GameObject Flash;


    void Update()
    {
        if (GlobalAmmo.LoadedAmmo >= 1)
        {

            if (Input.GetButtonDown("Fire1"))
            {
                AudioSource gunsound = GetComponent<AudioSource>();
                gunsound.Play();
                Flash.SetActive(true);
                StartCoroutine(MuzzleOff());
                GetComponent<Animation>().Play("GunShot");
                GlobalAmmo.LoadedAmmo -= 1;


            }
        }

        IEnumerator MuzzleOff()
        {
            yield return new WaitForSeconds(0.01f);
            Flash.SetActive(false);
        }
    }
}

Upvotes: 0

Views: 56

Answers (1)

Prateek Shrivastava
Prateek Shrivastava

Reputation: 1937

MuzzleOff() method is mistakenly put within the scope of Update() method. Please move it out as separate method.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunFire : MonoBehaviour
{

    public GameObject Flash;


    void Update()
    {
        if (GlobalAmmo.LoadedAmmo >= 1)
        {

            if (Input.GetButtonDown("Fire1"))
            {
                AudioSource gunsound = GetComponent<AudioSource>();
                gunsound.Play();
                Flash.SetActive(true);
                StartCoroutine(MuzzleOff());
                GetComponent<Animation>().Play("GunShot");
                GlobalAmmo.LoadedAmmo -= 1;


            }
        }        
    }

    IEnumerator MuzzleOff()
            {
                yield return new WaitForSeconds(0.01f);
                Flash.SetActive(false);
            }

}

Upvotes: 2

Related Questions