-3

Few question

  1. Is it require to place using System; on top of script in order to create a method that return string as outcome?
  2. Will using System stop Random.Range (0,2) from working?
  3. How to avoid CS0104 or in this case randomly return a String value in method?

Below are really simple script i am working on, suddenly i cannot use Random.Range CS0104 ,

"Random" is an ambiguous reference between "UnityEngine.Random" and "System.Random".

As you can see i am trying to create a method that random return ABC or AAA randomly

Please kindly advise a fix , thanks in advance!

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

public class GSC : MonoBehaviour

    void Start()
    {
        
    }


    void Update()
    {
        
    }

  public String StoreNameRoller() 
    {
        int abc = Random.Range(0, 2);
        if ( abc == 1)
        {
            return "ABC";
        }
        else
        {
            return "AAA";
        }
    }

Fix to CS0104 That related to using system in unity I do try according to suggestion, but then it saids System.Random.Range not exist blahblahblah

  • You may want to read https://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string-in-c... – Alexei Levenkov Apr 04 '23 at 18:07
  • Did you try fully qualifying the namespace of `Random`? i.e. `UnityEngine.Random.Range(0, 2)`. `System` has a class named `Random`, and so does `UnityEngine`. Having a `using` for both `System` and `UnityEngine` means it can't figure out which version of the `Random` class you're referring to. – Daniel Mann Apr 04 '23 at 18:08
  • @Alexei and Daniel thanks for the help, i don't even notice i use upper case lmao, guess thats why u don't code 2 Am in the morning, – Carta Issue Apr 05 '23 at 08:46

1 Answers1

1

Try using;

int abc = UnityEngine.Random.Range(0, 2);

Or use string instead of String and delete;

using System;

System and UnityEngine libraries both have Random function. When both of them are imported compiler cannot understand which Random you're trying to use.

If you want to use String and Random just specify which random you're going to use.

msaiduraz
  • 151
  • 5
  • 1
    Yep, this is the answer. And, just to add, if they wish to keep the System namespace, then they could always use the System's Random function. ---- E.g. for a random number between 0 and 100, they can use: Random rnd = new Random(); int randomNum = rnd.Next(0, 100); – DylanT 25 Apr 05 '23 at 07:38
  • 1
    That's right. Also you can specify that Random function will always be using from `that`library in this context. E.g. `using Random = UnityEngine.Random;` means Random functions in this context belong to UnityEngine.Random, or `using Random = System.Random;`. – msaiduraz Apr 05 '23 at 07:45