I started learning Unity this past weekend, and I’m having similar problems with preprocessor conditionals. I want to clear up clutter in the inspector by only providing declarations and access only to relevant variables. That way, I can increase the re-usability of my scripts while avoiding huge if statements. The following code returns a “Invalid preprocessor directive” and a “Single-line comment or end-of-line expected” on the lines following the #if and #elif directives. using UnityEngine; using System.Collections;
public class GunController : MonoBehaviour
{
public enum GunType { None, Projectile, Laser, Plasma }; // Behavior labels
public GunType type = GunType.Laser; // Selectable via drop-down menu
#if (type == GunType.Laser)
float power = 0; // Not relevant for a mass driver
#elif (type == GunType.Projectile)
float velocity = 0; // Not relevant for a laser
#else
// Other conditions follow
#endif
}
Is it not possible to use directives within a class? Am I asking for too much? My alternative is just to make separate classes for each type of weapon. Also, is this even the right approach? This would only work if the scripts are compiled on a per-use basis, which I don’t understand if they are or not. Do you have alternatives?