TransWikia.com

Unity: Add extra time to slider

Stack Overflow Asked by Christopher Madsen on November 28, 2020

So im making this door game, where you have to answer a riddle to get through the door. I have added a countdown slider to the game, and now I want to award the player with extra time, when a riddle is solved.

How can I say, when someone is answering correct on my riddle, there will be added 20s on the countdown.
Thanks for your help!

I have two scripts. The first is for the door and riddle:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class cakeriddle : MonoBehaviour {

public GameObject door;
public GameObject NextButton;
public GameObject riddleUI;
public Transform Player;

private string answer;
private string answerTwo;
private string guess;

void Awake()
{
sliderChanger = GameObject.FindObjectOfType<Slidertid>();
}

[SerializeField]
private InputField input;

[SerializeField]
private Text text;

void OnTriggerEnter(Collider other)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
riddleUI.SetActive(true);
answer = "cupcake";
answerTwo = "worldcup";
text.text = "What kind of cup can’t hold water?";
Player.GetComponent<FirstPersonController>().enabled = false;
}


public void GetInput(string guess)
{
CompareGuesses(guess);
input.text = "";
}

public void Next()
{
Time.timeScale = 1f;
Player.GetComponent<FirstPersonController>().enabled = true;
Cursor.visible = false;
}



void CompareGuesses(string guess)
{
if (guess.ToLower() == answer.ToLower())
{
text.text = "You Guessed Correctly";
NextButton.gameObject.SetActive(true);
GetComponent<Animator>().SetTrigger("DoorATrigger");
input.gameObject.SetActive(false);
door.GetComponent<BoxCollider>().enabled = false;
}
else if (guess.ToLower() == answerTwo.ToLower())
{
text.text = "You Guessed Correctly";
NextButton.gameObject.SetActive(true);
GetComponent<Animator>().SetTrigger("DoorATrigger");
input.gameObject.SetActive(false);
door.GetComponent<BoxCollider>().enabled = false;
}
else
{
text.text = "Wrong! Try look around for clues";
}
}
}

The next is for the slider:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class SliderChanger : MonoBehaviour {

public Slider slider;
private float time = 50f;
public Transform Player;

public GameObject gameOverUI;

void Start()
{
slider.maxValue = 50f;
slider.minValue = 0f;
}

void Update()
{
time -= Time.deltaTime;
slider.value = time;


if (slider.value <= 0)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
gameOverUI.SetActive(true);
Player.GetComponent<FirstPersonController>().enabled = false;
}

One Answer

There are a lot of ways to do this. Suiting for the code you provided you could use this simple solution. Change your time float to public.

public float time = 50f;

And in your start method set your slider max value to the value of the variable.

slider.maxValue = time;

Then after having a correct guess, add 20 seconds to time

sliderChanger.time += 20f;

Correct answer by Immorality on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP