- Home /
Random.Range Problems (Errors CS1502 and CS0266)
I am making a Splat The Rat style game in C#. For this game, I am using Random.Range a lot to determine where, when and what spawns. However, I am getting this mysterious error:
Error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
So I go and do the step that seems logical and switch the floats to integers in the Random.Range statement changing Random.Range (1.0f, 4.0f);
into Random.Range (1, 0);
Then I get error CS1502:
Error CS1502: The best overloaded method match for UnityEngine.Random.Range(float, float)' has some invalid arguments So I switch back to floats.
Random.Range (1.0f, 4.0f);` And I get CS0226 again.
Here's my entire script:
using UnityEngine;
using System.Collections;
public class Summoner : MonoBehaviour {
public int randomspawn;
public int randomNumber1;
public int randomNumber2;
public int randomnumber3;
public int randomnumber4;
public int randomnumber5;
public int randomnumber6;
public GameObject blueUnicorn;
public GameObject redUnicorn;
public GameObject purpleUnicorn;
public GameObject magentaUnicorn;
public GameObject pinkUnicorn;
public Transform spawnpos;
//spawn one = 2.4 3.12 0.77
//spawn two = 2.5 3.24 0.77
//spawn three = -2.25 -1.44 -0.77
//spawn four = 2.59 -1.32 -0.77
void Update () {
//choosing where to spawn
randomspawn = Random.Range (1, 4);
if (randomspawn == 1) {
spawnpos.transform.position = new Vector3 (2.4f, 3.12f, 0.77f);
}
if (randomspawn == 2) {
spawnpos.transform.position = new Vector3 (2.5f, 3.24f, 0.77f);
}
if (randomspawn == 3) {
spawnpos.transform.position = new Vector3 ( -2.25f, -1.44f, -0.77f);
}
if (randomspawn == 4) {
spawnpos.transform.position = new Vector3 (2.59f, -1.32f, -0.77f);
}
//choosing where to spawn end.
//chossing if to spawn
/*
*Picks a random number 1-10
*
*if it equals 5, it goes ahead.
*
* it picks a random number, 1 or 2
*
* if it is two, it summons a blue unicorn.
*
* if it is 1, it goes for red.
*
* it keeps going through until it reaches pinks.
*/
randomNumber1 = Random.Range (0.0f, 10.0f);
if (randomNumber1 == 5) {
randomNumber2 = Random.Range (1.0, 2.0);
if (randomNumber2 == 2) {
Instantiate (blueUnicorn, spawnpos.position, spawnpos.rotation);
}
if (randomNumber2 == 1) {
randomnumber3 = Random.Range (1.0, 2.0);
if (randomnumber3 == 2){
Instantiate (redUnicorn, spawnpos.position, spawnpos.rotation);
}
if (randomnumber3 == 1){
randomnumber4 = Random.Range (1.0, 2.0);
if (randomnumber4 == 2); {
Instantiate (purpleUnicorn, spawnpos.position, spawnpos.rotation);
}
if (randomnumber4 == 1); {
randomnumber5 = Random.Range (1.0, 2.0);
if (randomnumber5 == 2){
Instantiate (magentaUnicorn, spawnpos.position, spawnpos.rotation);
}
if (randomnumber5 == 1){
randomnumber6 = Random.Range(1.0, 2.0);
}
if (randomnumber6 == 2) {
Instantiate (magentaUnicorn, spawnpos.position, spawnpos.rotation);
}
if (randomnumber6 == 1){
return;
}
}
}
}
} else {
return;
}
}
}
Answer by tanoshimi · Jul 25, 2015 at 09:05 AM
Use ints everywhere.
public int randomspawn; << ok
randomNumber1 = Random.Range (0.0f, 10.0f); << not ok
if (randomNumber1 == 5) << ok
randomnumber4 = Random.Range (1.0, 2.0); << not ok
Your answer
Follow this Question
Related Questions
How to submit float score as time to Game Center? 1 Answer
how to make a float value,debug as int.(or show in text UI as Int) 1 Answer
Does genericList.Sort() go low to high? or high to low? - for ints/floats 1 Answer
How do you convert Random.Range to two decimal places? 1 Answer
What the hell? Cannot convert float to int PROBLEM. 2 Answers