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

Resolved Why is the "Can't convert type int to bool" Error coming up?

Discussion in '2D' started by Crazybrownie204, Aug 29, 2021.

  1. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Weapon : MonoBehaviour
    6. {
    7.     public Transform firePoint1;
    8.     public Transform firePoint2;
    9.     public Transform firePoint3;
    10.     public GameObject playerBulletPrefab;
    11.     public int playerShotNum = 0;
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         if (Input.GetMouseButton(0)){
    20.             if (playerShotNum = 2){
    21.                 Shoot();
    22.                 playerShotNum = 0;
    23.             }else{
    24.                 playerShotNum++;
    25.             }
    26.         }
    27.         void Shoot (){
    28.             Instantiate(playerBulletPrefab, firePoint1.position, firePoint1.rotation);
    29.         }
    30.     }
    31. }
    And for some reason, it keeps saying that line 22 ( if(playerShotNum = 2) { ) has the "Cannot convert type int to type bool" error. I am using the newest version of unity (V 2021.1.18f1), and no matter what I do, it just keeps popping up. Can someone please help?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Your if statement should be using double equals, which is equality test (takes two values and returns a bool).

    You are using a single equal which is assign and return assigned value, which is integer, hence the complaint.
     
  3. Crazybrownie204

    Crazybrownie204

    Joined:
    Mar 22, 2021
    Posts:
    23
    Thank You!
     
    Kurt-Dekker likes this.