Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help! Cannot implicitly convert type 'System.ConsoleColor' to 'UnityEngine.Color'

Discussion in 'Scripting' started by jeska2030, Oct 26, 2020.

  1. jeska2030

    jeska2030

    Joined:
    Oct 1, 2020
    Posts:
    5
    Please help!
    I searched for error for several days but I can't find.


    using System;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using UnityEngine;
    using UnityEngine.UI;
    using Random = UnityEngine.Random;

    public class GunController : MonoBehaviour
    {
    [Header("Gun Settings")]
    public float fireRate = 0.1f;
    public int clipSize = 30;
    public int reservedAmmoCapacity = 270;

    //Variable that change throughout code
    bool _canShoot;
    int _currentAmmoInClip;
    int _ammoInReserve;

    //Muzzle Flash
    public Image muzzleFlashImage;
    public Sprite[] flashes;

    private void Start()
    {
    _currentAmmoInClip = clipSize;
    _ammoInReserve = reservedAmmoCapacity;
    _canShoot = true;
    }

    private void Update()
    {
    if (Input.GetMouseButton(0) && _canShoot && _currentAmmoInClip > 0)
    {
    _canShoot = false;
    _currentAmmoInClip--;
    StartCoroutine(ShootGun());
    }
    else if (Input.GetKeyDown(KeyCode.R) && _currentAmmoInClip < clipSize && _ammoInReserve > 0)
    {
    int amounttNeeded = clipSize - _currentAmmoInClip;
    if (amounttNeeded >= _ammoInReserve)
    {
    _currentAmmoInClip += _ammoInReserve;
    _ammoInReserve -= amounttNeeded;
    }
    else
    {
    _currentAmmoInClip = clipSize;
    _ammoInReserve -= amounttNeeded;
    }
    }
    }

    IEnumerator ShootGun()
    {
    StartCoroutine(MuzzleFlash());
    yield return new WaitForSeconds(fireRate);
    _canShoot = true;
    }

    IEnumerator MuzzleFlash()
    {
    muzzleFlashImage.sprite = flashes[Random.Range(0, flashes.Length)];
    muzzleFlashImage.color = ConsoleColor.White;
    yield return new WaitForSeconds(0.05f);
    muzzleFlashImage.sprite = null;
    muzzleFlashImage.color = new Color(0, 0, 0, 0);
    }
    }
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    Try changing
    muzzleFlashImage.color = ConsoleColor.White;
    to
    muzzleFlashImage.color = Color.white;
     
    jeska2030 likes this.
  3. jeska2030

    jeska2030

    Joined:
    Oct 1, 2020
    Posts:
    5
    Thanks
     
    adamgolden likes this.