-3

here is my code:

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

public class Building : MonoBehaviour
{
    public enum State
    {
        Nothing,
        Burnt,
        Cold
    };

    private void Start()
    {
        var max = Enum.GetValues(typeof(State)).Length;
        var value = (State)new Random().Next(0, max - 1);
        Console.WriteLine(value); 
    }
}

I did some research and any answer I found, was either too confusing and I didn't know how to implement it into my script or it required me to delete Using System; or Using UnityEngine;. How can I fix this issue?

Thanks! :D

PS. I probably didn't do enough research and it was right in front of me this whole time, so please excuse that.

DavidA_
  • 93
  • 1
  • 14
  • You can either use a [namespace alias](https://stackoverflow.com/questions/505262/c-sharp-namespace-alias-whats-the-point) or move the `using System`/`using UnityEngine` into your `Building` class - depending on what `Random` you want to use. – devsmn Aug 22 '21 at 20:43
  • 1
    Simply put `using Random = UnityEngine.Random;` on top of your file – derHugo Aug 23 '21 at 11:32

1 Answers1

1

Random is an ambiguous reference in that case because both UnityEngine and System namespaces have a definition for Random.

You have to be more precise about which one you want to use:

var value = (State)new System.Random().Next(0, max - 1);

Or

var value = (State)new UnityEngine.Random().Next(0, max - 1);

Edit: As suggested by derHugo in the comment section you can define which definition you want to use. You can add this on top of the file:

 using Random = UnityEngine.Random;
Oak
  • 62
  • 1
  • 5