- Home /
is there a way to select the objects and highlight it when it meets the minimum condition from several slider?
Each blocks (game object ) will have 3 variable(Sq ft, No. of rooms, and price).
How do i make them highlight itself when they meet the minimum condition which is set by a slider?
For Example, if the slider is set to 900 Square ft, 3 rooms and 500k.
Anything above 900ft, 3 rooms and below 500k will be highlighted
Answer by WarmedxMints · Sep 20, 2019 at 04:51 AM
You could populate an array of the blocks and iterate through them when any slider is changed
public class Block : MonoBehaviour
{
public int SqaureFt;
public int RoomCount;
public int Price;
public void Highlight(bool highlight)
{
if(highlight)
{
//Set highlight colour
return;
}
//Set normal colour
}
}
public class Sliders : MonoBehaviour
{
//Populate list of blocks
public List<Block> Blocks;
public Slider SqFt, RoomCount, Price;
//Pass this method to all 3 sliders changed event
public void OnSliderValueChange()
{
if (Blocks == null || Blocks.Count == 0)
return;
var count = Blocks.Count;
var sqft = SqFt.value;
var rmCount = RoomCount.value;
var price = Price.value;
for(var i = 0; i < count; i++)
{
var block = Blocks[i];
var highlight = block.SqaureFt >= sqft && block.RoomCount >= rmCount && block.Price <= price;
block.Highlight(highlight);
}
}
}
everything seems to be working, however, 'Slider' can't be referenced.
"public Slider SqFt, RoomCount, Price;"
Do you know why?
If you are using Visual Studio, it will have a little light bulb icon which will give you a fix or you can press alt + enter with the default shortcuts.
You are missing;
using UnityEngine.UI;
Nice! There's no more error so far. You've been a great help! However. when i test play, the color does not change no matter how i slide. Am i missing something? Hope you can have a last look before i reward you :P.
I did everything u double slash except passing the method to those 3 sliders. How do i pass the method to those 3 sliders? Sorry for being a noob. :P
You either assign the method to the event via the inspector on the Slider component or you can do it in the Start method of the slider class with slider.onValueChanged.AddListener();
As for the colour change, you would have to implement that depending on your setup. Looking at your screenshots, I am guessing you just wish to swap materials which is done via the mesh renderer.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Prior Knowledge 0 Answers
Make a game in C# 0 Answers
How to start in Unity? 1 Answer
Mecanim Jump up HELP!! 1 Answer