- Home /
How to check if a script component exists before adding it?
I'm checking if a script exists before optionally using it like this:
MyClass myClass = go.AddComponent< MyClass >();
if(myClass != null){
// Do some stuff with it if it exists
}
else{
//no worries, continue anyway
}
But of course, if it doesn't exist, the main script won't compile in the first place, with an error: "The type or namespace name `MyClass' could not be found. Are you missing a using directive or an assembly reference?"
Is there a way around this?
Thanks
I'm not sure about your error, but to check, use GetComponent():
$$anonymous$$yClass myClass = go.GetComponent<$$anonymous$$yClass>()
if (myClass == null) {
myClass = go.AddComponent<$$anonymous$$yClass>();
}
Thanks, but that would still require checking if the script exists in the project first. It's tricky!
Answer by Kiwasi · Oct 10, 2014 at 12:38 AM
This is tricky. The answer involves using reflection. In general I tend to recommend avoiding reflection. I'll give you some terms and steps to help your google search. If I get some free time I'll come back and write the code.
Get your type name as a string
Check if the type exists
Check to see if the type is a component
Call add component with your type
Here are a few stack overflow questions to check out.
http://stackoverflow.com/questions/18650005/check-if-a-class-exists-within-the-current-net-assembly
http://stackoverflow.com/questions/2528216/how-to-detect-the-existence-of-a-class-at-runtime-in-net
Thanks very much for those links, I understand the problem better now, and it's clearly not trivial. Working through it now... (Apologies, dont have permission to upvote yet)
Answer by astracat111 · Oct 17, 2016 at 05:00 AM
To invoke a method by string name:
var assembly = Assembly.GetExecutingAssembly ();
var types = assembly.GetTypes();
foreach (var type in types) {
var methods = type.GetMethods ();
foreach (var method in methods) {
if (method.Name == "MyMethodTest") {
//Run method here
method.Invoke (null, null);
}
}
}
And to check if a method exists, returning 0 if there are no methods of that name anywhere in your assembly, 1 if there is a single one, and 2 if there are duplicate named ones:
int MethodExistsOrNot = 0;
var assembly = Assembly.GetExecutingAssembly ();
var types = assembly.GetTypes();
foreach (var type in types) {
var methods = type.GetMethods ();
foreach (var method in methods) {
if (method.Name == "MyMethodTest") {
//One more MyMethodTest exists
MethodExistsOrNot += 1;
}
}
}
if (MethodExistsOrNot > 1) {
Debug.Log ("There is more than one existence of this method. Methodname must be unique to invoke from assembly.");
}
if (MethodExistsOrNot == 0) {
Debug.Log ("No method exists in the assembly with that name.");
MethodExistsOrNot = 0;
}
if (MethodExistsOrNot == 1) {
Debug.Log ("Method exists!");
}
Debug.Log (MethodExistsOrNot);
The code will be slow and isn't optimized, but you can throw that into a function. Call the System.Reflection; namespace at the beginning of your file as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Attach C# Script on runtime 1 Answer
Copy a script from one gameobject to another? 1 Answer
AddComponent( 1 Answer