- Home /
Use a #if statement to check if a script exists in project?
Hi all, So I'm making a universal FPS related asset (I won't go into detail until its closer to release) that's made to more or less work with any FPS controller. However that means there will be some limitations to the asset. I was thinking I'd make it have more functionality with my own FPS controller if it was found to exist in both the project and the scene. I can do a check in the scene fine, but I'm having trouble doing a check in the project. it needs to happen in a #if because obviously if the controller's not in the project, it'd throw a ton of compiler errors.
Thanks for any advice!
What kind of file do you need to check? One of your owns? If so, I guess you can use a #define
in your own file, and check against this define in your other script.
Otherwise, in your scripts, you could add a define in the Project Settings when your assets get imported (like Text$$anonymous$$eshPro does)
Yeah It's one of my files, I just tried a #define and It didn't work, perhaps I did it wrong?
Script A: #define FPS_CONTROLLER_EXISTS
Script B: #if FPS_CONTROLLER_EXISTS Debug.Log("FPS Controller Found"); #endif
$$anonymous$$y bad, it seems the #define A
+ #if A
only works inside the same file.
You should take a look at PlayerSettings.SetScriptingDefineSymbolsForGroup
to define global defines when the project is loaded
https://docs.unity3d.com/ScriptReference/PlayerSettings.SetScriptingDefineSymbolsForGroup.html
Call this function thanks to an editor script with the InitializeOnLoadAttribute
https://docs.unity3d.com/ScriptReference/InitializeOnLoadAttribute.html
Otherwise, you may be able to use reflection, but I don't think this is the way to go
Answer by AedanCGraves · Sep 09, 2019 at 07:06 PM
@Hellium solved it in the comments, thanks!
Your answer
Follow this Question
Related Questions
c# Disregards my bool as if it doesn't exist? 1 Answer
Question on Possible FOR Loop Condition Conflict 0 Answers
c# Ignoring conditional statement? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers