Random.Range problem CS0226 cannot convert type float to int

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

public class SpikeGenerator : MonoBehaviour
{
    public GameObject spike;
    public float MinSpeed;
    public float MaxSpeed;
    public float CurrentSpeed;
    public float SpeedMultiplier;

    void Awake()
    {
        CurrentSpeed = MinSpeed;
        generateSpike();
    }

    void Update()
    {
        if (CurrentSpeed < MaxSpeed)
        {
            CurrentSpeed += SpeedMultiplier;
        }
    }

    public void GenerateNestSpikeWithGap()
    {
        int randomWait = Random.Range(0.1f, 1.2f);
    }

    public void generateSpike()
    {
        GameObject SpikeIns = Instantiate(spike, transform.position, transform.rotation);
        SpikeIns.GetComponent<SpikeScript>().spikeGenerator = this;
    }

}

@MertTheJojuk The Random.Range returns a float value as the range provided is float. So you are getting that error. You will have to parse the value to be an int and then it will work OR use int ranges. I have provided both the options for reference.

For Range to be as float and converted to int

public void GenerateNestSpikeWithGap()
{

    int randomWait = (int) Random.Range(0.1f, 1.2f);

}

For range to be integer itself.

public void GenerateNestSpikeWithGap()
{

    int randomWait = Random.Range(1f, 2f);

}

Hope this helps.

Random.Range has two overloads. One that takes two float arguments and returns a float value and another one that takes two integer arguments and returns an integer. Which method overload is used always depends just on the passed arguments, nothing else. So since your upper and lower bound are float values, the method also returns a float value.

You declared your “randomWait” variable as an integer. You can not implicitly convert a float to an int, you would always need an explicit cast. However it’s not really clear what your intention is here. When you cast a float to an int, the value can only be an integer, so the value is rounded towards 0. So a value like 1.2 will become 1 but also 1.1234 or 1.0 will be just 1. Any value below that would be just 0. So 0.943 or 0.2 they would all just round to 0. I don’t think that’s what you intended anyways.

A wait time usually should be a float, though it depends on the exact usecase. Currently you don’t do anything with that value, so we can’t really suggest what you should do.