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?

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.

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:

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:

  1. Select any script inside the Project tab.
  2. Click on the button “Execution Order” at the top right of the Inspector.
  3. Drag and Drop the HitReactionCharacter in the “Execution Order” (MonoManager) in the Inspector.
  4. Drag and Drop the HitReactionCharacter above the “Default Time” separator.