InvalidCastException: Cannot cast from source type to destination type.

Trying to make a small section of code that spawns 3d text objects at certain locations, but I keep getting this error

part of script 1.

 DebugTextScript Text = (DebugTextScript)Instantiate(TextBoxToInstantiate, neighbour.worldPosition, Quaternion.identity);
            Text.gValue = neighbour.gCost;
            Text.hValue = neighbour.hCost;
            Text.fValue = neighbour.fCost



using UnityEngine;
using System.Collections;

public class DebugTextScript : MonoBehaviour {

    public int gValue;
    public int hValue;
    public int fValue;
    public GameObject Textbox;

    public DebugTextScript( int _gValue, int _hValue, int _fValue)
        gValue = _gValue;
        hValue = _hValue;
        fValue = _fValue;

        //ThisText.text = gValue.ToString() + "

" + hValue.ToString() + "
" + fValue;


How is “TextBoxToInstantiate” declared? If it doesn’t look like this:

public DebugTextScript TextBoxToInstantiate;

Of course you will get a casting exception.

Since you haven’t included the relevant part we can only guess.