[SOLVED] I can't update a text according to difficultySlider

I want to update my “Diff” text acoording to “difficultySlider”. I added some code to my OptionsController.cs at the below. But it doesn’t work. What is my fault or what should i add more. Can u help me pls. Thanks


OptionsController.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class OptionsController : MonoBehaviour {

public Slider volumeSlider, difficultySlider;
public LevelManager levelManager;
public Text diffMode;

private MusicManager musicManager;  
  
void Start () {
	musicManager = GameObject.FindObjectOfType<MusicManager>();

	volumeSlider.value = PlayerPrefsManager.GetMasterVolume();
	
	difficultySlider.value = PlayerPrefsManager.GetDifficulty();
	
	
	diffMode = GameObject.FindObjectOfType<Text>();
	
}


void Update () {
	musicManager.ChangeVolume(volumeSlider.value);
	
	
	    if(difficultySlider.value== 1f)
			diffMode.text = "EASY";
			
		else if(difficultySlider.value == 2f)
			diffMode.text = "NORMAL";
			
		else if(difficultySlider.value == 3f)
			diffMode.text = "HARD";
	

	}

public void SaveAndExit(){ 
	PlayerPrefsManager.SetMasterVolume(volumeSlider.value);
	PlayerPrefsManager.SetDifficulty(difficultySlider.value); 
	levelManager.LoadLevel("01a Start");
}

public void SetDefault(){ //Default Button

	volumeSlider.value = 1f;
	difficultySlider.value = 2f;
}

}

You should replace this:

diffMode = GameObject.FindObjectOfType< Text >();

for:

diffMode = GameObject.Find(“Diff”).GetComponent< Text >();

or, i dont know if this gonna work:

diffMode = GameObject.FindObjectOfType< Diff >();

ignore the spaces between the < >

tried but doesn’t work. Thanks

Hi,
It looks like you could remove this line entirely from your Start() method:

diffMode = GameObject.FindObjectOfType();

You declared diffMode as ‘public’ and in the inspector already assigned the proper text object to diffMode, no need to find it in the Start() method.

Also,
diffMode = GameObject.FindObjectOfType();
is actually looking for a Text object on the Options Controller GameObject. If you didn’t assign in the inspector you would want to first reference the correct GameObject and then find it’s Text Component.

Other than that it looks good but if not try posting the error message as well.

2 Likes

Wow thanks, deleting diffMode = GameObject.FindObjectOfType(); from Start() worked. And also i realized a bit bug or something im not sure. When i trying play the game in 01b Options Scene my code still doesn’t work but i tried it from 00 Splash scene again (first scene) and its worked. Thanks so much.

1 Like