Search Unity

Question how to fix cannot implicitly convert float to bool error

Discussion in 'Scripting' started by Gryffinax, Feb 13, 2024.

  1. Gryffinax

    Gryffinax

    Joined:
    Feb 13, 2024
    Posts:
    4
    i am trying to make a knockoff chrome dinosaur game and i get this error here is the full code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class spawnshits : MonoBehaviour
    {
    public GameObject bigshit;
    public GameObject doublebigshit;
    public GameObject smallshit;
    public GameObject triplesmallshits;
    public float spawnRate = 2;
    private float timer = 0;
    public float heightOffset = 0;
    private float spawn = 0;

    // Start is called before the first frame update
    void Start()
    {
    spawnPipe();
    }

    // Update is called once per frame
    void Update()
    {

    if (timer < spawnRate)
    {
    timer = timer + Time.deltaTime;
    }
    else
    {
    spawnPipe();
    timer = 0;
    }
    }

    void spawnPipe()
    {
    spawn = spawn + Random.Range(1,4);

    if (spawn = 1)
    {
    float lowestPoint = transform.position.y - heightOffset;
    float highsetPoint = transform.position.y + heightOffset;
    Instantiate(bigshit, new Vector3(transform.position.x, Random.Range(lowestPoint, highsetPoint), 0), transform.rotation);
    }

    if (spawn = 2)
    {
    float lowestPoint = transform.position.y - heightOffset;
    float highsetPoint = transform.position.y + heightOffset;
    Instantiate(doublebigshit, new Vector3(transform.position.x, Random.Range(lowestPoint, highsetPoint), 0), transform.rotation);
    }

    if (spawn = 3)
    {
    float lowestPoint = transform.position.y - heightOffset;
    float highsetPoint = transform.position.y + heightOffset;
    Instantiate(smallshit, new Vector3(transform.position.x, Random.Range(lowestPoint, highsetPoint), 0), transform.rotation);
    }

    if (spawn = 4)
    {
    float lowestPoint = transform.position.y - heightOffset;
    float highsetPoint = transform.position.y + heightOffset;
    Instantiate(triplesmallshits, new Vector3(transform.position.x, Random.Range(lowestPoint, highsetPoint), 0), transform.rotation);
    }






    }

    }
    ignore height offset and stuff like that the spawn code was ripped from an old project of recreating flappy bird
    the error happens on the lines with the if (spawn = whatever number
     
  2. Gryffinax

    Gryffinax

    Joined:
    Feb 13, 2024
    Posts:
    4
    forgot to write full error it is error CS0029: Cannot implicitly convert type 'float' to 'bool'
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    One equals (
    =
    ) is used to assign, two (
    ==
    ) are used to compare equality. I would brush up on your coding basics before going much further.

    Mind you, a float is rarely every going to exactly equal an integer value due to floating point precision. You probably want to be using integers for
    spawn
    instead.
     
    Bunny83 likes this.
  4. Gryffinax

    Gryffinax

    Joined:
    Feb 13, 2024
    Posts:
    4
    ty fam i will try that
     
    spiney199 likes this.
  5. Gryffinax

    Gryffinax

    Joined:
    Feb 13, 2024
    Posts:
    4
    it worked