SetParent after Instantiate. NullReferenceException error

Hello guys. I’m making simple 2D clicker game i have really annoying problem.

I’m trying make popup text after Click, but my “GPC(Clone)” creates outside of Canvas and not visible.
I’m trying fix that with this code:
gpcClone.transform.SetParent(MainCanvas.transform);

But when i make a click in Game mode, Console write: “NullReferenceException: Object reference not set to an instance of an object”
And “GPC(Clone)” again creates outside of Canvas and not visible.

Help me, how i can fix that?

My code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Click : MonoBehaviour {

	public UnityEngine.UI.Text gpc;
	public UnityEngine.UI.Text gemDisplay;
	public UnityEngine.UI.Text asteroidHealthBar;
	public float gem = 0.00f;
	public int gemperclick = 1;
	public int asteroidFullHealth = 100;
    public GameObject MainCanvas;  // I'm drop in this Game Object my Canvas

	void Update() {
		gemDisplay.text = "" + gem.ToString ("F0");
		gpc.text = "+" + gemperclick;
		asteroidHealthBar.text = asteroidFullHealth + " / 100";
	}

	public void Clicked() {
		gem += gemperclick;
		asteroidFullHealth -= gemperclick;
		float x = Random.Range(-150, 150);
		float y = Random.Range(100, 200);
		float z = 0;
		gpc.transform.localPosition = new Vector3 (x, y, z);
		GameObject gpcClone = Instantiate (gpc, gpc.transform.localPosition, Quaternion.identity) as GameObject;
		gpcClone.transform.SetParent(MainCanvas.transform);
	}

}

I’ve noticed in your code that the following in your click class:

gpc.transform.localPosition = new Vector3 (x, y, z);

Comes before you Instantiate a clone of gpc.

GameObject gpcClone = Instantiate (gpc, gpc.transform.localPosition, Quaternion.identity) as GameObject;

Instantiate first…