Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to lock slider increase?

I have two sliders. and what I want to happen is that when value of both of the sliders is equal to 50 then I want player to not be able to increase slider value further, however still be able to decrease the value. another thing I think I would have to do is to make one of slider value decrease to 50 when value of both sliders is higher than 50.


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class SliderLock : MonoBehaviour

{

  public Slider AssasinLock;

  public Slider DestroyerLock;

  public float LockNumber = 50;

  public float sum;

  public SpawnAssasin assasinscript;

  public SpawnEnemy enemyscript;


  // use this initialization

  void Start()

  {    

  }


  // Update is called once per frame

  public void Update()

  {

    sum = AssasinLock.value + DestroyerLock.value;

    if(LockNumber <= sum)

    {

      DestroyerLock.interactable = false;

      AssasinLock.interactable = false;

    }

    else

    {

      DestroyerLock.interactable = true;

      AssasinLock.interactable = true;

    }

  }

}

Best Answer

  • MouledouxMouledoux Member
    Accepted Answer

    Regardless of the answer to my previous edit, this should be enough to get you going, even if it's not exactly what you described.

    This can let you link an unlimited number of sliders to share 1 'bank' value. The sliders combined total cannot exceed the bank limit, and sliders can only increase if there is still some left in the bank. If a slider is decreased, that value go back to the bank. The only thing this doesn't do that I think you may have wanted, is if you decrease a slider, that value doesn't get distributed to the other sliders. It just goes back to the bank.

Answers

  • MouledouxMouledoux Member
    edited June 2020

    That's so crazy, I just wrote this script last night, I'm afk now but gimmie like 30m and I'll post it


    Edit: quick question though, could you explain

    "another thing I think I would have to do is to make one of slider value decrease to 50 when value of both sliders is higher than 50."

    A bit more? Are the sliders able to go past 50? I thought their combined total was 50 or less. Meaning only 1 slider could ever be at 50.

Sign In or Register to comment.