- Home /
How to get access to C# script in another way.
Hello smart people,i have very simple question for you.
How can i disable a C# script FROM ANOTHER script?
Yeah,yeah i know,i know there are TONS of answers and solutions out there that can make this work...
Im going to show you my script and afterwards the problem.
So here are some lines from my script :
public HitReactionCharacter Hitreaction;
Hitreaction = GameObject.FindWithTag("Avatar").GetComponent<HitReactionCharacter>();
This script up there is correct and it should work fine,but it does not... I got error : The type or namespace name `HitReactionCharacter' could not be found. Are you missing a using directive or an assembly reference?
I KNOW how can i fix this error,I know in order to FIND and Get ACCESS TO specific c# script (from other script),i have to put that specific script in a particular folder, like "Plugins" "Standart Assets".... So then the script will find that "specific" script which needs to enabled/disabled/changed...
But i cannot do that,because if i change the script destination,everything will be messed up for me,because my script is connected to the other scripts,and if i drag that script in that special folder,i will Lost the whole connection and everything gets messed up. I Absolutely CANNOT afford to change my script location...
So here is the real question for you Guys: How can i GET ACCESS TO C# script in different way?
Answer by spiceboy9994 · May 22, 2015 at 02:52 PM
What's the path of your script HitReactionCharacter. It may not be an issue with the path, but more a Namespace issue. Namespaces are logical groups for classes. Go to your HitReactionCharacter and check what namespace is specified just before the class defition. It should be something like
namespace MyCustomNamespace.SubNamespace
{
public class HitReactionCharacter: MonoBehavior
{
}
}
It could or could not have a sub namespace. The only thing that you need to do, on all the classes where you want to use that script is to include the namespace with a using Statement on the top of the class file.
using MyCustomNamespace.SubNamespace;
By adding that using, you should not have that issue anymore.
This is how C# works in terms of classes and namespaces. Hope this works for Unity.
Hello spiceboy9994,i tried doing this thing with namespaces before,i didnt work,also i cant afford to do that,cause my script already have namespace which holds other scripts.
Thanks for your help?
Answer by pako · May 22, 2015 at 05:55 PM
The problem seems to be with the order of compilation of scripts. This is why the problem could be solved, if you placed your script in a "Special Folder". Here's a reference if you want more info:
http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
So, what seems to be happening is that the following instruction in your code:
GameObject.FindWithTag("Avatar").GetComponent<HitReactionCharacter>();
...runs before HitReactionCharacter script compiles.
This is why you get the error message that "type or namespace name `HitReactionCharacter' could not be found". It's not found, because it hasn't compiled yet.
Since you want a workaround, you can use the sript "Execution Order" as follows:
Select any script inside the Project tab.
Click on the button "Execution Order" at the top right of the Inspector.
Drag and Drop the HitReactionCharacter in the "Execution Order" (MonoManager) in the Inspector.
Drag and Drop the HitReactionCharacter above the "Default Time" separator.
Hi Pako,very nice explanation you have there, anyway i did everything you said i should do,but still i get error massage : "type or namespace name `HitReactionCharacter' could not be found" :(
Hello People,just a quick question,is there any way to do this with VARIBALES? I tried this before,with method:
public Component HitReactionCharacter;
HitReactionCharacter = GameObject.FindWithTag("Avatar").GetComponent("HitReactionCharacter");
The thing is that it WOR$$anonymous$$S,it finds the component as script. BUT,when i try to manipulate the code,like,you know disable and enable it (just what i need) : HitReactionCharacter.enabled = false;
It shows error : Type UnityEngine.Component' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
I think i Understand what the error means and i dont like that fact.
This seems very simple,handy method. But it does not want to work,maybe you guys know some other methods like this one?
Thumbs up for people who help,Thanks,i very appreciate that!
You cannot enable/disable components. You can only enable/disable the GameObject that a component is attached to, using GameObject.SetActive(true/false):
http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
This means that even if you didn't get the error using the code in your question, it wouldn't work for you, because Hitreaction.enabled = false would throw an error (similar to what you go using Component).
So, maybe you have to re-think how you want to do things.
Other than that, the change you made (and worked) has nothing to do with the variable type, but because you used a different from of GetComponent:
A. GetComponent<HitReactionCharacter>() //original code - does not work
B. GetComponent("HitReactionCharacter")()// changed code - works
Usually, you use form 'A' for better performance, but form 'B' if you are trying to access a C# script from Javascript, because it's not possible to get the type of a C# script from Javascript (hence your original error and why it worked when you changed it to form 'B'):
http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
It wasn't clear to me that you were trying to access a C# script from Javascript. I thought you had 2 C# scripts.