using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RangeCheckCubeScript : MonoBehaviour
{
public float Range = 1;
public Slider Slider;
public void RangeUpdate(float UpdatedRange)
{
Range = UpdatedRange;
Debug.Log("CalledRangeUpdate");
}
}
Hello. I am currently working on trying to change a value (Range) using Unity’s slider object. I want the slider component to call the RangeUpdate function every time it is changed but it does not call the function. I used a Debug.Log to check but there is nothing in the console when I change the slider. I’ve not been having luck trying to find answers online because they almost all tell me to use a Dynamic float (which I am). Does anyone know why it is not calling RangeUpdate? Is it because I’m using an old version of Unity (Unity 2020.3.14f1)? If there is any more information and context I can provide you, I am glad to oblige. Thank you for your time.
Just to make sure, slider callbacks will be invoked if you change the value in the game or by code, but will not be invoked if you change the value on the inspector. Are you trying to invoke this method by changing the slider in the inspector by any chance?
@Nefisto I’m not too sure if I understand your question properly or not. By changing the slider in the inspector, you mean changing the slider in Unity before I press the play game button correct? If so, I am not trying to do that and I am just trying to use the slider while I am running the game.
@ My bad. That was a typo. The script I show is indeed RangeCheckScript. I’ll correct it. My bad
@Kazfish What I’m trying to say is that if you use this slider
In-game or not the event will not be invoked, I only asked to make sure that the slider that you’re referring to is the one in-game view.
If this isn’t the case, you probably have made some mistake on the references, can you share more info, hierarchy with the canvas that contains the slider, maybe the inspector of the GO that has this component attached
Here is a screenshot of the hierarchy with the Canvas + Slider that the slider component is attached to. Is there any more information I can provide you with?
I don’t know what to say, this should just work, what I suggest is that you start from the ground in a temporary scene, this way you will be able to find what is wrong because this is a really simple setup and should just work. If it does not find what happened send me a pm and we try to figure it out together.
@mgear I did cut some other parts of the script because I thought it was irrelevant to the post but I’ll post the full script here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RangeCheckCubeScript : MonoBehaviour
{
public float Range = 1;
public Slider Slider;
void CheckForTiles(GameObject SelectedTile)
{
Collider[] TileObjects = Physics.OverlapBox(SelectedTile.transform.position, new Vector3(Range * 4 / Mathf.Sqrt(2), 10, Range * 4 / Mathf.Sqrt(2)) / 2, Quaternion.Euler(0,45,0));
for (int i = 0; i < TileObjects.Length; i++)
{
if (TileObjects[i].tag == "Tile")
{
TileObjects[i].SendMessage("ActivateSupportGrid");
}
}
}
public void RangeUpdate(float UpdatedRange)
{
Range = UpdatedRange;
Debug.Log("CalledRangeUpdate");
}
}
I don’t believe any of the things I cut out do interfere with the slider.
Also, there are no warning messages in the console and I am using the slider in the game and not in the inspector. @Nefisto Thank you very much for attempting to help me. I appreciate your kindness. I’ll check tomorrow (Because I am somewhat busy today) in a new Unity file if it works there and I’ll tell you if I find something interesting. Thanks
Update: I have found the solution. In the project settings, I had enter play mode options on. The slider didn’t work with it on so if anyone is having the same trouble I am, check that off. Thank you to all of you that helped me.