- Home /
Menu Building - Cannot implicity convert type float to int
Back again with a new noobie question.
So I'm building this main menu Rust style and I found a quality slider code for my options menu. Totally overjoyed I copy pasted it in my code to find the error stated below. If someone could point out the error and explain WHY this keeps happening you will probably never see this question again! Thank you in advance! Code btw is in C#
Error
Assets/Scripts/mainMenuSliderScaleable.cs(68,49): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
Code using UnityEngine; using System.Collections;
public class mainMenuSliderScaleable : MonoBehaviour
{
//resolution settings
public float originalWidth = 1024.0f;
public float originalHeight = 768.0f;
private Vector3 scale;
//scrollbar settings
public Vector2 scrollPosition;
//menu settings
public bool showOptions = false;
public bool showCredits = false;
public Texture2D MyTexture;
//video settings
public int gameQuality = 0;
//audio settings
public float masterVolume = 100.0f;
public float musicVolume = 100.0f;
public float SFXVolume = 100.0f;
void Update ()
{
AudioListener.volume = musicVolume / 100.0f;
}
void OnGUI ()
{
scale.x = Screen.width / originalWidth;
scale.y = Screen.height / originalHeight;
scale.z = 1;
var svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
//GUI.Box (new Rect (0, 0, Screen.width, Screen.height));
GUILayout.BeginArea (new Rect (20, Screen.height / 2 - 50, 200, 100));
GUILayout.BeginVertical ();
if (GUILayout.Button ("Start Game")) {
Application.LoadLevel (4);
}
if (GUILayout.Button ("Options")) {
showOptions = true;
showCredits = false;
}
if (GUILayout.Button ("Credits")) {
showCredits = true;
showOptions = false;
}
if (GUILayout.Button ("Quit Game")) {
Application.Quit ();
}
GUILayout.EndVertical ();
GUILayout.EndArea ();
if (showOptions == true) {
GUILayout.BeginArea (new Rect (300, 50, 600, 800));
//scrollbar
scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (600), GUILayout.Height (600));
//video
//quality
GUILayout.Label ("Video Settings", GUILayout.Width (100));
GUILayout.BeginHorizontal ();
GUILayout.Label ("Quality", GUILayout.Width (100));
GUILayout.BeginVertical ();
gameQuality = GUILayout.HorizontalSlider (gameQuality, 0, 5,GUILayout.Width(150), GUILayout.Height(35));
QualitySettings.SetQualityLevel (gameQuality);
GUILayout.EndVertical ();
GUILayout.EndHorizontal ();
//resolution
GUILayout.Label ("", GUILayout.Width (100));
//800x600
//1024x768
//1366x768
//1920x1080
//audio
GUILayout.Label ("Audio Settings", GUILayout.Width (100));
GUILayout.Label ("Master Volume", GUILayout.Width (100));
masterVolume = GUILayout.HorizontalSlider (masterVolume, 0.0f, 100.0f);
GUILayout.Label ("Music Volume", GUILayout.Width (100));
musicVolume = GUILayout.HorizontalSlider (musicVolume, 0.0f, 100.0f);
GUILayout.Label ("Effects Volume", GUILayout.Width (100));
SFXVolume = GUILayout.HorizontalSlider (SFXVolume, 0.0f, 100.0f);
//controller
GUILayout.Label ("Control Controls", GUILayout.Width (100));
GUILayout.Label ("Move Controls", GUILayout.Width (100));
GUILayout.Label ("Look Controls", GUILayout.Width (100));
GUILayout.Label ("Interaction Controls", GUILayout.Width (100));
GUILayout.EndScrollView ();
GUILayout.EndArea ();
GUI.matrix = svMat;
}
}
}
When you post code, especially code that's referencing an error and a line# at least format all the code so the two line-up. As is they are off cuz you didn't include all your code in your formatting
lol As I am pretty new to unity answers it's pretty harsh that you already flame me for a mistake. However my problem is still there.
Answer by chaos_technician · Feb 05, 2014 at 07:46 AM
That error means that you're calling a function or operator that expects an integer type, but you're supplying a float instead. It looks to me like the issue is on this line:
gameQuality = GUILayout.HorizontalSlider (gameQuality, 0, 5,GUILayout.Width(150), GUILayout.Height(35));
In your code, gameQuality
is an integer, whereas GUILayout.HorizontalSlider
returns a float. You need to cast the result from GUILayout.HorizontalSlider
to an int
when assigning it to gameQuality
.
gameQuality = (int)GUILayout.HorizontalSlider (gameQuality, 0, 5,GUILayout.Width(150), GUILayout.Height(35));
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Horizontal Slider with Labels 3 Answers
How to make a slider menu 1 Answer
"NullReferenceException: Object reference not set to an instance of an object" with instantiate C# 1 Answer