title: How to access a variable from another script in another gameobject through GetComponent?
body: <p>I've searched around and I just can't get this to work. I think I just don't know the proper syntax, or just doesn't quite grasp the context.</p>
  
  <p>I have a BombDrop script that holds a public int. I got this to work with public static, but Someone said that that is a really bad programming habit and that I should learn encapsulation. Here is what I wrote:</p>
  
  <p><em>BombDrop script:</em></p>
  
  <pre><code> &lt;!-- language: c# --&gt;
  
   public class BombDrop : MonoBehaviour {
  
   public GameObject BombPrefab;
  
   //Bombs that the player can drop
   public int maxBombs = 1;
  
  
   // Update is called once per frame
   void Update () {
  
   if (Input.GetKeyDown(KeyCode.Space)){
  
           if(maxBombs &gt; 0){
           DropBomb();
  
           //telling in console current bombs
           Debug.Log("maxBombs = " + maxBombs);
           }
     }
  
  
   }    
  
   void DropBomb(){
  
       // remove one bomb from the current maxBombs
       maxBombs -= 1;
  
       // spawn bomb prefab
       Vector2 pos = transform.position;
       pos.x = Mathf.Round(pos.x);
       pos.y = Mathf.Round(pos.y);
       Instantiate(BombPrefab, pos, Quaternion.identity);
  
   }
  }
  </code></pre>
  
  <p>So I want the Bomb script that's attached to the prefabgameobject Bombprefab to access the maxBombs integer in BombDrop, so that when the bomb is destroyed it adds + one to maxBombs in BombDrop.</p>
  
  <p>And this is the Bomb script that needs the reference.</p>
  
  <pre><code> public class Bomb : MonoBehaviour {
   // Time after which the bomb explodes
   float time = 3.0f;
  
   // Explosion Prefab
   public GameObject explosion;
  
   BoxCollider2D collider;
  
   private BombDrop BombDropScript;
  
  
  
  
   void Awake (){
  
       BombDropScript = GetComponent&lt;BombDrop&gt; ();
   }
  
   void Start () {
  
       collider = gameObject.GetComponent&lt;BoxCollider2D&gt; ();
  
       // Call the Explode function after a few seconds
       Invoke("Explode", time);
  
   }
  
   void OnTriggerExit2D(Collider2D other){
  
       collider.isTrigger = false;
   }
  
   void Explode() {
       // Remove Bomb from game
       Destroy(gameObject);
  
       // When bomb is destroyed add 1 to the max 
       // number of bombs you can drop simultaneously .
       BombDropScript.maxBombs += 1;
  
       // Spawn Explosion
       Instantiate(explosion,
                   transform.position,
                   Quaternion.identity);
  </code></pre>
  
  <p>In the documentation it says that it should be something like</p>
  
  <pre><code>BombDropScript = otherGameObject.GetComponent&lt;BombDrop&gt;();
  </code></pre>
  
  <p>But that doesn't work. Maybe I just don't understand the syntax here. Is it suppose to say otherGameObject? Cause that doesn't do anything. I still get the error : "Object reference not set do an instance of an object" on my BombDropScript.maxBombs down in the explode()</p>