We are going to need the rest of your code, it is unclear what your variables are used for. What is this IndexEngine variable? It seems that your index value does never reach a maximum that would reset it.
As mentioned, your code doesn’t make sense. Why do you use Index in all your if statements except the last which uses IndexEngine? Are you sure you didn’t mean Index? And if that isn’t the case, then it increases probably because IndexEngine == 3 and never changes.
I’m confused at what you expect to happen. You are resetting index to 3 even when it goes above 3, so every click will still execute the index == 3 code. So slide.value will increase. If you don’t want it to increase the slide.value, then you need to check it’s value before adding to it. Otherwise, don’t keep resetting index back to 3.
would there be any way to obtain the value that is within your index 3, do not increase more? because if I keep pressing the button the slider keeps increasing?
This code always runs and it always says to take the slide.value and ADD valor[3] to it and THEN set the slide.value equal to that new number.
So if slide.value is 1 and valor[3] is 2 then after that code runs, slide.value is 3. Then when it runs again, slide.value is 5. It is adding valor[3] every single time this if statement runs. So the code is working exactly how you tell it to. If you don’t want it to add the value, you either need to set slide.value = valor[3] or you need to let index move beyond 3 so this if statement doesn’t run after Index changes.
Let’s start of by cleaning up this mess, it’s way too confusing. Then we’ll be able to think about the problem.
public class Yolo
{
// This probably has no reason to be public
int index = 0;
// This probably shouldn't be public either but i'll do for now
public float[] values; // and let's name it correctly
public Slider slide; // Your class is name slider and your variable slide... it's confusing at best
// C# convention uses Upper case for mtehdos
public void Next()
{
// Is it normal by the way that your variables here had an uppercase but not in the properties?
index++;
// Do we have reasons to believe that the index could be lower than zero without public access?
// if (Index <= 0)
// {
// Index = 0;
// }
if (index >= values.Length)
{
index = 0;
}
// Probably shoudl add some guard here if we have to reasons to believe that the array could be empty
slide.value = values[index];
}
}
That may not fix it. Does OP want to set the value or add the value? And does OP want to add it only once or set it only once? The request is a bit unclear.
OP, I would suggest if you want Next to stop, then before index++, check the value and if it’s >= values.Length, you just return. @Magnesium has a lot of good suggestions on how to clean up your code, you just need to clarify if you want to add, set equal to, loop, whatever.