title: Unity disable particle system when outside of camera
body: <p>I am making a 2D game in unity that has an underwater theme. To make it look really pretty, I use a script to spawn glowy objects at random locations in the map. These objects are blank sprites with Particle Systems attached to them.</p>
  
  <p>The problem is, I have about 100 of them active in the map at a time, and though it looks really pretty, the game lags on computers that don't have an i5 processor or faster.</p>
  
  <p>The solution I thought of was to disable the objects while they are out of camera bounds so there is only about 4 or 5 active at a time...</p>
  
  <p>Here's my script:</p>
  
  <pre><code>void Update () {
  
      if (this.transform.position.x &lt; Camera.main.transform.position.x + 13 &amp;&amp; this.transform.position.x &gt; Camera.main.transform.position.x - 13 &amp;&amp; this.transform.position.y &lt; Camera.main.transform.position.y + 13 &amp;&amp; this.transform.position.y &gt; Camera.main.transform.position.y - 13) {             this.gameObject.particleEmitter.emit = true;
          this.gameObject.particleSystem.Play ();
  
  
              } 
  
      else {
  
          this.gameObject.particleSystem.Pause ();
  
  
      }
  } 
  </code></pre>
  
  <p>Ok, it checks if the object is outside the camera bounds correctly, but when it comes to disabling the object, I get this error:</p>
  
  <p>"NullReferenceException: Object reference not set to an instance of an object"</p>