- Home /
[JS] - GetComponents (No appropriate version)
So I've got multiple components attached to a gameobject called 'Gun_Module' and I'm going to make an array and search through the array and switch off specific ones.
private var Gun_Module : Component;
private var GunScripts : Component[];
GunScripts = GetComponents(Gun_Module);
The error is that it says:
"No appropriate version of 'UnityEngine.Component.GetComponents' for the argument list '(UnityEngine.Component)' was found."
Help would be greatly appreciated!
Answer by taxvi · Dec 10, 2014 at 07:01 AM
ok, i guess Gun_Module is your class right? than it can not be a name of a variable, rather try this:
private var GunScripts : Gun_Module[];
GunScripts = GetComponents(Gun_Module);
It states the name 'Gun_$$anonymous$$odule' does not denote a valid type ('not found')
oh, I just noticed that you said Gun_$$anonymous$$odule is a gameObject, I thought it was a name of the script. thing is, by using GetComponents() you can not retrieve all the components of the gameObject, but rather retrieve only the components of SPECIFIC type. Like, if your object has two colliders attached to it than GetComponent(Collider) will give you all the Colliders of the object. but each script attached to the game object is of a separate type. Like if you have two scripts on your enemy called Defense$$anonymous$$odeScript and Attack$$anonymous$$odeScript you need to call the GetComponents() once for each script. I'd also advise you to use the gameObject.GetComponent ins$$anonymous$$d, like:
myComponent1 = myGameObject.GetComponent(myScriptname1);
myComponent2 = myGameObject.GetComponent(myScriptname2);
...
etc.
I know it's a little pain in the ass but this is the best solution I can think of.
hmm, thanks for the long response but the 'Gun_$$anonymous$$odule' is the name of the script. There are multiple 'Gun_$$anonymous$$odule' scripts attached to the single gameObject. This single gameObject has this script which searches through the array. :/ I'm a bit stumped at this. Also, why would it say it does not denote a valid type 'not found'?
Thanks for the help so far.
nice, good to know such stuff, I just gained a better understanding how to integrate C#/JS scripts better :))