- Home /
How to change the color from code
So i am making a game and i have a player pref saved with the level that they have completed which determines the colour, green means that it is completed yellow not completed and red locked, this is my code so far
public class LevelButtons : MonoBehaviour {
private int LevelCompleted;
void Start() {
LevelCompleted = PlayerPrefs.GetInt("LevelCompleted");
if (int.Parse(this.tag)<=LevelCompleted) {
this.GetComponent(text.color) = color.green;
}
}
}
it dosent seem to be working at the moment and the code looks like it should work, please could sombody tell me how to do this right.
Answer by ShadyProductions · Jul 28, 2017 at 05:06 PM
You don't need to use the 'this' word everytime, you're already in the class.
this is only used when you are trying to access variables that have the same name but occur otherwhere like in a constructor
private int blabla
public Constructor(int blabla) {
this.blabla = blabla;
}
Following cases are used for this:
To qualify members hidden by similar name.
To have an object pass itself as a parameter to other methods.
To have an object return itself from a method.
To declare indexers.
To declare extension methods.
To pass parameters between constructors.
Also text.color is not a component.
GetComponent(text.color)
A component is, like a transform, or a collider, or a script, or a rigidbody. I think you need to look for the Text component or whatever it is that you are looking for.
GetComponent<Text>().color = color.green;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Smooth Camera Background Color Changing? 1 Answer
SpriteRender.color not changing the color 1 Answer
Distribute terrain in zones 3 Answers
Change startcolor of particle system through script 1 Answer