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?
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.
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.
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.
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.
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