error CS0266: Cannot implicitly convert type `int' to `HelloWorld.Weaponsd'. An explicit conversion

I am trying to create a simple script. I get this error : error CS0266: Cannot implicitly convert type int' to HelloWorld.Weaponsd’. An explicit conversion exists (are you missing a cast?) Can you please figure it out?

Code (This code gives out an error) :

using UnityEngine;
using System.Collections;

public class HelloWorld : MonoBehaviour {
    public enum Weaponsd{None = 0, Pistol = 1, ShotGun = 2, Sniper = 3};
    public Weaponsd Weapons ;
    public int Ammos = 10;
    public int Bullets = 1;
    public int NumberOfBullets = 3;

    void FireWeapon (int NumberOfBullets)
    {
        if (Weapons != 0) {
                        for (int i=0; i<NumberOfBullets; i++) {
                                if (Ammos > 0) {

                                        Debug.Log ("Weapons Fired");
                                        Ammos = Ammos - 1;
                                } else {
                                        Debug.Log ("No Ammo");
                }
                               
                        }
                } else {
            Debug.Log("No Weapon");       
        }
        }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.Space)) {
            if (Weapons = 1) {
                FireWeapon(1);
            }
   
    }
}
}

Code(This Code doesn’t give out error):

using UnityEngine;
using System.Collections;

public class HelloWorld : MonoBehaviour {
    public enum Weaponsd{None = 0, Pistol = 1, ShotGun = 2, Sniper = 3};
    public Weaponsd Weapons ;
    public int Ammos = 10;
    public int Bullets = 1;
    public int NumberOfBullets = 3;

    void FireWeapon (int NumberOfBullets)
    {
        if (Weapons != 0) {
                        for (int i=0; i<NumberOfBullets; i++) {
                                if (Ammos > 0) {

                                        Debug.Log ("Weapons Fired");
                                        Ammos = Ammos - 1;
                                } else {
                                        Debug.Log ("No Ammo");
                }
                               
                        }
                } else {
            Debug.Log("No Weapon");       
        }
        }

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.Space)) {
            if (Weapons != 0) {
                FireWeapon(1);
            }
   
    }
}
}

At line 37, it should be

if (Weapons == 1)

instead of

if (Weapons = 1)

Edit: You don’t need to specify the corresponding integer in your enum :

public enum Weaponsd{None, Pistol, ShotGun, Sniper};

Also, you should not use integer to know what value has your “Weapons” variable but the enum name directly :

if (Weapons == Weaponsd.Pistol)
1 Like