Random.Range won't work for me

Hello I am having trouble with Random.Range not working as shown in the video. I’m probably just overlooking something blindingly obvious as normal. If you have any suggestions or solutions let me know.

Here is the code I am using.

SHOOTER.CS

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

public class Shooter : MonoBehaviour
{
[Header(“General”)]
[SerializeField] GameObject projectilePrefab;
[SerializeField] float projectileSpeed = 15f;
[SerializeField] float projectileLifeTime = 5f;
[SerializeField] float baseFiringRate = 0.3f;

[Header("AI")]
[SerializeField] float minimumFiringRate = 0.1f;
[SerializeField] float firingRateVariance = 0f;
[SerializeField] bool useAI;

[HideInInspector] public bool isFiring;

Coroutine firingCoroutine;

void Start()
{
    if (useAI)
    {
        isFiring = true;
    }
}
void Update()
{
    Fire();
}

void Fire()
{
    if (isFiring && firingCoroutine == null)
    {
        firingCoroutine = StartCoroutine(FireCountinuously());
    }
    else if(!isFiring && firingCoroutine != null)
    {
        StopCoroutine(firingCoroutine);
        firingCoroutine = null;
    }
}

IEnumerator FireCountinuously()
{
    while (true)
    {
        GameObject instance = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
        Rigidbody2D rb = instance.GetComponent<Rigidbody2D>();
        if(rb != null)
        {
            rb.velocity = transform.up * projectileSpeed;
        }
        Destroy(instance, projectileLifeTime);
        float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance, baseFiringRate + firingRateVariance);
        timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minimumFiringRate, float.MaxValue);
        yield return new WaitForSeconds(timeToNextProjectile);
    }
}

}

Hi,

I dont know what you changed in the editor, or what exactly your problem is;
but if you did not change the value of “firingRateVariance”
it would be zero and nothing wil be deducted or added,
so then the numbers u use to get a random between are the same.

Well I followed what was shown in the video and the firingRateVariance was set to 0 in the code but changed later in the inspector.

The issue seems to be that Random.Range just isn’t accepted as an option where it is in the code. It is underlined by a red squiggly line and any solutions offered by Visual Studio don’t resolve the issue. I can’t find any spelling errors or other problems that would result in Random.Range not being accepted.

So I’m stumped.

The math code looks fine to me.

What are the values in the appropriate variables?

Show me a picture of what you’re seeing of this suiggly line.

Here are two screen shots one of the issue in the code and the other the inspector error and enemy character with the attached scripts.

I’m sure it’s just something minor but important I’m overlooking, I just can’t find it.

It is because you are using System; and using UnityEngine;. Random exists in both namespaces and Visual Studio can’t guess which one you want to use. I don’t really see a need for System so just remove it

Thank you. I would have never realized that was the cause.

Sometimes you may need to be using both System and UnityEngine. In those cases you’d have to fully qualify it by writing UnityEngine.Random.Range(...) (or System.Random depending on which one you want to use).

Another option I’ve seen once was to alias the Random you want to use, essentially removing the other

using Random = UnityEngine.Random;

Also, if you hover over the red squiggly it will (should) tell you that Random is ambiguous between UnityEngine.Random and System.Random

The error on the bottom tells you the issue. It cannot tell if you’re talking about UnityEngine.Random.Range and System.Random.Range. The issue happens when you have both using UnityEngine and using System.

Change it to UnityEngine.Random.Range and you should be good to go.

[Damn, didn’t see Bix’s reply before I posted. Whoops]

Thanks you all for the help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms