Reputation: 350
I am trying to set a Slider value through C# code.
I have a Main.scene
which includes a slider called Slider.
The main camera has a game manager script attached to it.
The slider is not Interactable (so I can display the value but the user can't change it).
I defined a variable:
[SerializeField] private Slider sliderObj;
and put my slider in the inspector in this field
I have a float s
variable that I want to reflect in the slider:
s = player1Time / (player1Time + player2Time) * 100.0f;
but when I write:
sliderObj.value = s;
I see this error:
'Slider' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type 'Slider' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]
any ideas?
Here is the entire script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null; //this is to create a singleton => only one instance of this script running in memory
//so we only have one Game Manager running on memory
private int activePlayer = 1; //1 = down position 2 = up position
[SerializeField]
private GameObject playerObj;
[SerializeField]
private Text timer1Text; //player1 timer text
[SerializeField]
private Text timer2Text; //player2 timer text
[SerializeField]
private GameObject startBtn;
[SerializeField]
private Texture stopTexture;
[SerializeField]
private Slider sliderObj;
private Slider sli;
public float sli_val;
private RawImage img;
private float startTime;
private bool player1Active = true;
private static float player1Time;
private static float lastP1Time;
private static float player2Time;
private static float lastP2Time;
private bool gameOver = false;
private bool gameStarted = false;
private bool gameFinished = false;
void Awake()
{
if (instance == null)
{ //check if an instance of Game Manager is created
instance = this; //if not create one
}
else if (instance != this)
{
Destroy(gameObject); //if already exists destroy the new one trying to be created
}
DontDestroyOnLoad(gameObject); //Unity function allows a game object to persist between scenes
}
public bool GameStarted
{
get { return gameStarted; }
}
public bool GameOver
{
get { return gameOver; }
}
public void StartGame()
{
if (!gameStarted)
{
gameStarted = true;
//startTime = Time.time;
//player1Time = startTime;
player1Active = true;
//select start button
img = (RawImage)startBtn.GetComponent<RawImage>();
//replace it with stop button
img.texture = (Texture)stopTexture;
}
else
{
gameStarted = false;
gameOver = true;
}
Debug.Log("StartGame");
}
public void ChangePlayer()
{
float ty = 0f;
int smileyRotate = 180;
int sliderRotate = 180;
Quaternion smileyRotation = playerObj.transform.localRotation;
ty = playerObj.transform.position.y * -1;
if (activePlayer == 1)
{
player1Active = false;
activePlayer = 2;
smileyRotate = 180;
}
else
{
player1Active = true;
activePlayer = 1;
smileyRotate = 0;
}
playerObj.transform.position = new Vector2(playerObj.transform.position.x, ty);
smileyRotation.x = smileyRotate;
playerObj.transform.localRotation = smileyRotation;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
private string FormatTime(float pt)
{
string hours, minutes, seconds, shaon;
hours = ((int)pt / 3600).ToString("00");
minutes = ((int)pt / 60).ToString("00");
seconds = (pt % 60).ToString("00");
if ((pt / 3600) > 1)
{
shaon = string.Format("{0:D1}:{1:D2}:{2:D2}", hours, minutes, seconds);
}
else
{
shaon = string.Format("{0:D2}:{1:D2}", minutes, seconds);
}
return shaon;
}
void Update()
{
float s;
GameObject sl1 = GameObject.Find("Slider");
if (GameStarted)
{
string zman;
if (player1Active)
{
player1Time += Time.deltaTime; //if player 1 active update his time
zman = FormatTime(player1Time);
timer1Text.text = zman;
//Debug.Log("player2Time: : "+ player2Time);
}
else
{
player2Time += Time.deltaTime; //if player 2 active update his time
zman = FormatTime(player2Time);
timer2Text.text = zman;
Debug.Log("player1Time: : " + player1Time);
}
s = player1Time / (player1Time + player2Time) * 100.0f;
//sliderObj.GetComponent<Slider>;
//sliderObj.=0.5f;
sli = GameObject.Find("Slider").GetComponent<Slider>();
sli.value = s;
sliderObj.value = s;
}
}
}
Upvotes: 2
Views: 7996
Reputation: 350
First of all, thanks for all your help.
Specially to bpgeck that chat with me and help me a lot.
Also Programmer was correct.
The main problem I had was the fatal error of calling the script attached to my slider Slider (I am still a noob, but learning)
I deleted that script and created a new one called Slider Percentage, in it I use the update function to change the value of the slider:
GameObject temp;
// Use this for initialization
void Start () {
temp = GameObject.Find("Slider");
}
// Update is called once per frame
void Update () {
temp.GetComponent<Slider>().value = GameManager.instance.GetPercentage;
}
The percentage value is still being calculated in the GameManager, but it is displayed using this script.
Now all works!! Thanks again for all your help :-)
Upvotes: 2