- Home /
Problems using NGUI for in-game options sliders
Hi guys,
I have the following script on a GameObject named Menu Manager in my main menu scene:
var gameSceneName : String;
var sensitivitySlider : GameObject; var musicSlider : GameObject; var soundSlider : GameObject;
var sound : float; var music : float; var sensitivity : float;
function Start () { sound = 0; sound += PlayerPrefs.GetFloat("SoundVol");
music = 0;
music += PlayerPrefs.GetFloat("MusicVol");
sensitivity = 0;
sensitivity += PlayerPrefs.GetFloat("Sensitivity");
}
function Update () {
}
function PlayGame () { Application.LoadLevel(gameSceneName); }
function QuitGame () { Application.Quit(); }
function OnSensitivitySliderChange () { PlayerPrefs.SetFloat("Sensitivity", sensitivitySlider.sliderValue); Debug.Log( "Sensitivity Slider Changed" + PlayerPrefs.GetFloat("Sensitivity")); }
function OnSoundSliderChange () { PlayerPrefs.SetFloat("SoundVol", soundSlider.sliderValue); Debug.Log( "Sound Slider Changed" + PlayerPrefs.GetFloat("SoundVol")); }
function OnMusicSliderChange () { PlayerPrefs.SetFloat("MusicVol", musicSlider.sliderValue); Debug.Log( "Music Slider Changed" + PlayerPrefs.GetFloat("MusicVol")); }
The scene also has an options panel with three sliders on it. The three sliders in the script are set to the three sliders on my options panel. Also, the Event Receiver for each of the three sliders is Menu Manager. When I run the scene and pull up the options panel, moving the sliders throws this error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) MenuManager.OnMusicSliderChange () (at Assets/Menus/MenuManager.js:54) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) UISlider:Set(Single, Boolean) (at Assets/NGUI/Scripts/Interaction/UISlider.cs:293) UISlider:UpdateDrag() (at Assets/NGUI/Scripts/Interaction/UISlider.cs:212) UISlider:OnDragThumb(GameObject, Vector2) (at Assets/NGUI/Scripts/Interaction/UISlider.cs:161) UIEventListener:OnDrag(Vector2) (at Assets/NGUI/Scripts/Internal/UIEventListener.cs:41) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) UICamera:ProcessTouch(Boolean, Boolean) (at Assets/NGUI/Scripts/UI/UICamera.cs:837) UICamera:ProcessMouse() (at Assets/NGUI/Scripts/UI/UICamera.cs:695) UICamera:Update() (at Assets/NGUI/Scripts/UI/UICamera.cs:576)
This happens for each and every slider. It says some object isn't set, but the receiver is set for the sliders, the relevant functions are set for each slider, and the sliders in Menu Manager are set. Even stranger is the fact I can'r subclass the sliders to NGUI's UISlider class. Does the script have to be rewritten in C# for it to work properly, or is something else wrong?
MachCUBED
The most immediate problem I can see here is that musicSlider is declared as a GameObject and you're trying to access its .sliderValue which I'm assu$$anonymous$$g is an NGUI thing? You might need a GetComponent, or NGUI might be sending you the object as part of a delegate call somewhere. You should probably see if it's doing that before you try a GetComponent. I'm surprised, though, that it's even compiling to be honest. (Oh, and check that you're assigning those musicSlider variables in the inspector properly, too!)
I'm just going to ask them and post the link here when it's fixed.
They say that it's awaiting approval, but here's the link anyway: http://www.tasharen.com/forum/index.php?topic=609.0 it turns out that the GameObjects need to be UISliders, but making them UISliders in Javascript causes compile time errors. $$anonymous$$aybe the script needs to be redone in C#.
FIXED: See above link for details, the script must be in C#.
@$$anonymous$$achCUBED Did you fix your issue there, chaging to C#? If so, you really should answer your own question and accept it! ;-)
Your answer
Follow this Question
Related Questions
Unity3D Game Time 1 Answer
How to make a good options menu. 1 Answer
How to make text appear when moving next to an object? 3 Answers
GameObjects name As GuiText 2 Answers
create a GuiSkin 2 Answers