Can't Understand GetComponent C#

Hi there.
I realize it’s my fault here, because I’m just too dummy to understand how it works from the tons of examples everybody can easily find here or in other sites.

How do I use GetComponent?
I’ve been in the docs, I’ve visited a lot of questions, I’ve seen a lot of examples but still I’m getting errors when it comes to write one on my own.

Here is what I’ve understood of how GetComponent works:
You got 2 scripts, script A and script B, there’s no need of a parent/child relation.
You declare a new public variable in script A, go to script B and then you write…?

For example:
I want to make a script to handle all common variables of my enemies.
That way I won’t have to write those variables for each one of them, it makes sense?
Variables like enemySpeed, enemyDamage, and so on.
Done that, I create a new c#script and call it with the specific name of an enemy, like Thug, I make it a child of my Enemy script by typing Public Class Thug : Enemy{} and I now want to access and modify all the values I’ve already written in Enemy.
How do I do that? Is this reasonable?

Sorry this is a very basic question but I’m unable to get past it… and because of this, could you please be exhaustive in your answers? like, really verbose and full of examples?
Keep in mind that all the existing examples weren’t enough to make me understand… I may have clenched that this process is easier in JavaScript though.

Thanks a lot for the patience.

You use GetComponent to access something you need in other script.

This is how it works, simple example.

//First script
public class Enemy : MonoBehaviour{    
   public int health = 10;

   public void saySomething(){
     Debug.Log("Hi, i am an enemy.");

//Second script
//There is one thing though, 
//if the script is located on another gameobject, 
//which will be most likely, you have to somehow 
//find that gameobject first. There are many ways of 
//doing it, via raycast, collision, 
//trigger, find by tag, find by gameobject etc. 

//But i will use the simplest way to understand, 
//that is declaring public GameObject in player script, 
//which contains Enemy, and later you just drag and drop the 
//gameobject in the inspector.

public class Player : MonoBehaviour{
   //Drag and drop gameobject with script Enemy in inspector
   GameObject enemyGameObject;
   void Start(){
      //We get Enemy component - script 
      //from public gameobject which contains this script
      Enemy enemyScript = enemyGameObject.GetComponent<Enemy>();

      //This will debug Enemy health is: 10
      Debug.Log("Enemy health is:"+ 

      //This will debug "Hi, i am an enemy."

Try this, in one of your scripts:

Public static bool test;

In your other script under e.g. void FixedUpdate(){

Getcomponent <NameofScript> ();

If (getMouseButtonDown(0) {

NameofScript.test = true; // Look what I did, called that variable from another script :)


If you get the logic behind that, it all falls into place… It’s good for calling variables between scenes etc.