So, I have a script that, when isSelected = true, the object turns green. Works fine. if (isSelected) {blahblahblah change color blahblahblah} So that is how i did it. However, I want the object to return to its normal material (not tinted green) when isSelected = false. In theory the game should do that automatically, as the above line of code is in the update function. However, even after I deselect the object, the color is still green. Weird. Help?
I would assume you changed the actual material in your project, rather than an instance of the material on an object. --Eric
What Eric5h5 means is that if you have a reference to a material (dragged into a public variable in your script) and you change that, then you will change the actual on-disk asset. Before using such a variable in your script, you should (in your Start() function), make a copy of it, which you would then change in your game. Code (csharp): public Material TheMaterial; void Start() { // replace the material reference with an instance TheMaterial = new Material( TheMaterial); } // and from now on you can use it however you like, it's just a copy... This is not without its caveats: if you have many scripts doing this, then each one will have its own drawcall, and that will impact performance. You would then need to do it only once and have all such objects share that new material instance.
Code (CSharp): //You have to change it back, as well renderer.material.color = (isSelected) ? selectedColor : defaultColor;
No no this is what i mean Code (csharp): #pragma strict var tile1 : GameObject; var tile2 : GameObject; var tile3 : GameObject; var tile4 : GameObject; var tile5 : GameObject; var tile6 : GameObject; var isSelected : int; var c : Color; // 0 = false 1 = true function Start () { PlayerPrefs.SetInt("isSelected", 0); } function Update () { if (isSelected == 1) { gameObject.renderer.material.color = Color.green; tile1.renderer.material.color = Color.yellow; tile2.renderer.material.color = Color.yellow; tile3.renderer.material.color = Color.yellow; tile4.renderer.material.color = Color.yellow; tile5.renderer.material.color = Color.yellow; tile6.renderer.material.color = Color.yellow; } } function OnMouseDown () { if (PlayerPrefs.GetInt("isSelected") == 0) { isSelected = 1; PlayerPrefs.SetInt("isSelected", 1); } } function OnGUI () { if (isSelected == 1) { if (GUI.Button(Rect(Screen.width/1.2 - (Screen.width/10)/2, Screen.height/3 - (Screen.height/10)/2, Screen.width/15, Screen.height/15), "Deselect")) { isSelected = 0; PlayerPrefs.SetInt("isSelected", 0); } } } But my issue is that when I press the deselect button the tiles are still tinted green and yellow.
then your update function should look like this: Code (JavaScript): function Update () { if (isSelected == 1) { gameObject.renderer.material.color = Color.green; tile1.renderer.material.color = Color.yellow; tile2.renderer.material.color = Color.yellow; tile3.renderer.material.color = Color.yellow; tile4.renderer.material.color = Color.yellow; tile5.renderer.material.color = Color.yellow; tile6.renderer.material.color = Color.yellow; } else { gameObject.renderer.material.color = defaultColor; tile1.renderer.material.color = defaultColor; tile2.renderer.material.color = defaultColor; tile3.renderer.material.color = defaultColor; tile4.renderer.material.color = defaultColor; tile5.renderer.material.color = defaultColor; tile6.renderer.material.color = defaultColor; } } But those game objects should really be in a list or an array so you can loop through them, rather than setting each one individually
Ohhh ok, thank you. But is defaultColor a variable? Because if so that won't work But wont this make my game lag like hell? because I have hundreds of tiles with this script on them, and it is constantly processing the materials color if I use an else statement, am I correct? And how does an array work? I have heard alot about them but they seem quite complicated.
Arrays are a very important and very basic tenant of computer programming. I suggest you start here: http://www.w3schools.com/js/js_arrays.asp As for your question, the renderer has to process the material every frame regardless of what changes you make to it. The amount of time it takes the carry out an assignment operation (in this case, telling it what color to be) is going to be negligible. EDIT: That being said, Eric (below) is right. You should put the content of your Update function inside of your OnMouseDown function, to eliminate redundancy.
You really shouldn't use Update unless you need things to change every single frame, though. The code for changing the material should only run when the mouse is clicked, not every frame. --Eric
Yes, but the defaultColor can not actually be a default color. I tried setting a Color variable, but the closest thing I could get to a "default" color is black, grey, clear, and white. Black makes my tiles black, grey makes them grey (ew), clear makes them black, and white makes them look like ultra high gamma lol. So what is a "default" color? Because I can not seem to find it on the color spectrum
defaultColor is supposed to be a variable. You would set it like so: Code (JavaScript): var defaultColor : Color; function Awake() { defaultColor = gameObject.renderer.material.color; }
Oh wait nevermind. White is the default color on the color spectrum... lol... well thanks for the help guys, case closed