I don’t use it often(duh) but after 20 mins i wonder,…
void ChangeMat()
{
boolValue ? rend.material = Mat_1 : rend.material = Mat_2;
}
I don’t use it often(duh) but after 20 mins i wonder,…
void ChangeMat()
{
boolValue ? rend.material = Mat_1 : rend.material = Mat_2;
}
without knowing what’s not working about it … use sharedmaterial not material. material makes a duplicate copy of the material and uses that instead.
so if you’re altering your material after you’ve assigned it and wondering why you’re not seeing the changes … that’s why. the renderer has a different material.
oh nonono, it doesn’t even compile, says
“Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement”
it’s that boolValue, but bool is a condition, i am khonfusion
Hi @Reedex
Ternary operator returns something, so you’ll have to formulate your code like this:
var boolValue = true;
int setValue = 0;
// if boolValue is true, return 1 else return 0
setValue = boolValue ? 1 : 0;
Debug.Log("SetValue:" + setValue);
So in this case, setValue variable could be your renderer’s material
Thanks, don’t know why i didn’t try this.silly : - )
The ?: operator (C# reference) isn’t used that way, @Reedex .
Effectively, this ternary operator replaces this:
bool myVariable;
if(condition){
myVariable = true;
} else {
myVariable = false;
}
It does not, however, replace this:
if(condition){
someMethodA();
} else {
someMethodB();
}
and it also does not allow you to replace the IF-Statement as you try in your original statement.
Correct would be:
if(boolValue){
rend.material = Mat_1 :
} else {
rend.material = Mat_2;
}
Which is then shortened to:
rend.material = boolValue ? Mat_1 : Mat_2;
C-Sharp has some syntax sugar that greatly shortens what you have to write, but not as much as you try in your original post ![]()
yes i gathered by now (testing gave wrong results)
I will use your source-post later : - ) Thanks
oh, nevermind i got it, your very last snippet made it click : - ) Thanks again
You are welcome! When running into such issues, I highly recommend to look into Microsoft’s documentation and programming guides, as I linked to the ternary operator before. They almost always provide detailled, yet simple examples.
well i did, i do post on forum (mostly) as a last resort
but this example of theirs
is this condition true ? yes : no
made me think i do it the right way…
boolValue ? doThing_A : doThing_B;
because how could this condition be true? (rhetoric question(to an extent))
rend.material = boolValue
@Reedex , I myself have fallen for that, too. It is important to not scan over the articles too quickly, since for instance “is this condition true ? yes : no” implies a lot but actually only means a very particular thing. Thus, I recommend to do read through the coded examples, where they use the stuff themselves.