title: Using code in other class
body: <p>My code works perfectly in the player class, but when I try to put it in a different class, it does nothing. My player doesn't lose lifes, and he doesn't die.</p>
  
  <p>Here it doesn't work:</p>
  
  <p><em>Spikes class</em></p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class Spikes : MonoBehaviour
  {
    SimplePlayer0 player = new SimplePlayer0();
    Animator anim;
    private bool isDead;
  
    void OnCollisionEnter2D(Collision2D other)
    {
      if (other.gameObject.tag == "spike" &amp;&amp; !isDead &amp;&amp; player.Lives &lt;= 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives = 0;
        isDead = true;
      }
      else if (other.gameObject.tag == "spike" &amp;&amp; player.Lives &gt;= 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives--;
        anim.SetBool("Immume", true);
        player.IsImmune = true;
      }
    }
  
  
  
  
    void OnTriggerStay2D(Collider2D other)
    {
      if (other.tag == "spike" &amp;&amp; !isDead &amp;&amp; player.Lives &lt;= 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives = 0;
        isDead = true;
      }
  
      else if (other.gameObject.tag == "spike" &amp;&amp; player.Lives &gt;= 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives--;
        anim.SetBool("Immume", true);
        player.IsImmune = true;
      }
    }
  
  
    void OnCollisionStay2D(Collision2D other)
    {
      if (other.gameObject.tag == "spike" &amp;&amp; !isDead &amp;&amp; player.Lives &lt;= 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives = 0;
        isDead = true;
      }
  
  
      else if (other.gameObject.tag == "spike" &amp;&amp; player.Lives &gt; 1 &amp;&amp; !player.IsImmune)
      {
        player.Lives--;
        anim.SetBool("Immune", true);
        player.IsImmune = true;
      }
  
      else if (other.gameObject.tag == "Underground")
      {
        isDead = true;
      }
  
    }
  
    void Dead()
    {
      if (isDead == true)
      {
        Application.LoadLevel(8);
      }
    }
  
  }
  </code></pre>
  
  <p><em>Player class</em></p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class SimplePlayer0 : MonoBehaviour
  {
    Animator anim;
  
  
  
    //Lives
    private int lives = 3;
    private bool isImmune;
    public float immuneCounter;
    public float immuneTime;
  
  
  
    //PROPERTIES
  
    public int Lives 
    {
      get { return lives; }
      set { lives = value; }
    }
  
    public bool IsImmune
    {
      get { return isImmune; }
      set { isImmune = value; }
    }
  
  
    void Start ()
    {
      anim = GetComponent&lt;Animator&gt;();
    }
  
    void Update()
    {
  
      if (IsImmune)
      {
        immuneCounter -= Time.deltaTime;
      }
      if (immuneCounter &lt;= 0)
      {
        IsImmune = false;
        immuneCounter = immuneTime;     
        anim.SetBool("Immume", false);
      }
  
  
  
  }
  </code></pre>
  
  <p>This was the original code where it did work:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class Player0 : MonoBehaviour
  {
    Animator anim;
  
  
    //Lives
    public int lives = 3;
    public bool isImmune;
    public float immuneCounter;
    public float immuneTime;
  
  
    public bool isDead;
  
  
  
    void Start()
    {
      anim = GetComponent&lt;Animator&gt;();
    }
  
  
    void Update()
    {
  
      if (isImmune)
      {
        immuneCounter -= Time.deltaTime;
      }
      if (immuneCounter &lt;= 0)
      {
        isImmune = false;
        immuneCounter = immuneTime;       
        anim.SetBool("Immume", false);
      }
  
      if (isDead == true)
      {
  
        Application.LoadLevel(8);
  
      }
  
  
  
    void OnCollisionEnter2D(Collision2D other)
    {
      if (other.gameObject.tag == "spike" &amp;&amp; !isDead &amp;&amp; lives &lt;= 1 &amp;&amp; !isImmune)
      {
  
  
        rigidbody2D.velocity = Vector2.zero;
        lives = 0;
  
        rigidbody2D.AddForce(new Vector2(0, 200)); // death animation
        isDead = true;
  
  
      }
      else if (other.gameObject.tag == "spike" &amp;&amp; lives &gt;= 1 &amp;&amp; !isImmune)
      {
        lives--;
        anim.SetBool("Immume", true);
        isImmune = true;
      }
    }
  
  
  
  
    void OnTriggerStay2D(Collider2D other)
    {
      if (other.tag == "spike" &amp;&amp; !isDead &amp;&amp; lives &lt;= 1 &amp;&amp; !isImmune)
      {
  
  
        rigidbody2D.velocity = Vector2.zero;
        lives = 0;
        rigidbody2D.AddForce(new Vector2(0, 200));
        isDead = true;
      }
      else if (other.gameObject.tag == "spike" &amp;&amp; lives &gt;= 1 &amp;&amp; !isImmune)
      {
        lives--;
        anim.SetBool("Immume", true);
        isImmune = true;
      }
  
      else if (other.gameObject.tag == "Underground")
      {
        rigidbody2D.AddForce(new Vector2(0, 200));
        isDead = true;
      }
    }
  
  
  
  
    void OnCollisionStay2D(Collision2D other)
    {
      if (other.gameObject.tag == "spike" &amp;&amp; !isDead &amp;&amp; lives &lt;= 1 &amp;&amp; !isImmune)
      {
        rigidbody2D.velocity = Vector2.zero;
        lives = 0;
  
        rigidbody2D.AddForce(new Vector2(0, 200));
        isDead = true;
  
  
      }
      else if (other.gameObject.tag == "spike" &amp;&amp; lives &gt; 1 &amp;&amp; !isImmune)
      {
        lives--;
        anim.SetBool("Immune", true);
        isImmune = true;
      }
      else if (other.gameObject.tag == "Underground")
      {
        rigidbody2D.velocity = Vector2.zero;
        lives = 0;
  
  
        isDead = true;
      }
  
    }
  
  }
  </code></pre>