Hi all, this little code here stumps me. It works just fine but I get compiler warnings.
Maybe I am not seeing the forest for the trees ![]()
The compiler throws a “warning CS0162: Unreachable code detected” on me in the “for(…)” loop. It seems to be disturbed by the return in the loop as if it thinks it will always return at first try (which may, or may not be true depending on the result of the if check).
It als gives me a “warning CS0642: Possible mistaken empty statement” warning on the “if(..)” line which I think is the root cause. However, I don’t get how it can be an “empty” statement (always true).
public SerializedProperty GetSerializedPropertyOfEffect(string id, string propertyName)
{
var so = new SerializedObject(this);
var effects = so.FindProperty("_effects");
for (int i = 0; i < effects.arraySize; i++) // <- i++ is "unreachable"
{
var effect = effects.GetArrayElementAtIndex(i);
if (effect.FindPropertyRelative("Id").stringValue == id); // <- is an "empty" statement
{
var property = effect.FindPropertyRelative(propertyName);
return property;
}
}
return null;
}
Just for fun I tried this:
public SerializedProperty GetSerializedPropertyOfEffect(string id, string propertyName)
{
var so = GetSerializedObject();
var effects = so.FindProperty("_effects");
for (int i = 0; i < effects.arraySize; i++) // <- it still thinks "i++" is unreachable.
{
var effect = effects.GetArrayElementAtIndex(i);
// v- IDE tells me this will always be true which confirms my suspicion. But WHY?
if (i%2 == 0);
{
var property = effect.FindPropertyRelative(propertyName);
return property;
}
}
return null;
}
I assume it has something to do with “.arraySize” of SerializedProperty but if I decompile it I can not really see an issue:
/// <summary>
/// <para>The number of elements in the array.</para>
/// </summary>
public int arraySize
{
get
{
this.Verify(SerializedProperty.VerifyFlags.IteratorNotAtEnd);
return this.GetInspectableArraySize();
}
set
{
this.Verify();
this.ResizeArray(value);
}
}
[MethodImpl(MethodImplOptions.InternalCall)]
private extern int GetInspectableArraySize();
I mean, the array size is not a constant (why should it be) but then how does to compiler determine that it will always be 0 (which I assume it does based on the warnings I see)?
I can solve this on another way (and probably will even though the code is working just fine). I am just curious. What the heck am I missing? ![]()
UPDATE: Never mind, 10+ years of coding and still bested by an extra “;”
![]()
Can you find it ? ![]()