Implicit conversion error

Hi all,

I’m having an annoying implicit conversion error in my code. I have tried to change everything I did in a program from JavaScript to c# and managed to get 50 errors which I have now managed to condense to one more error.

The error is "Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’.

The error is occuring on a line of code that is trying to attach one game object to another, this being the code :

cubeClass.pegObjects[n].transform.parent = cubeClass.cubeObj;

which is within this block of code :

void OnMouseDown () {
		for(int n = 0; n < cubeClass.pegObjects.Length + 1; n++) {
			if(pegObjectsClass.isConnectedToMouse == true) {
				pegObjectsClass.isConnectedToMouse = false;
				cubeClass.pegObjects[n].transform.position = slotPosition;
				cubeClass.pegObjects[n].transform.parent = cubeClass.cubeObj;
				pegObjectsClass.isAttached = true;
				hasPegAttached = true;
			}
		}
	}

and the pegObjects and cubeObj are both originally declared in the cube class as such :

static public GameObject cubeObj;
static public GameObject[] pegObjects;

Any help is greatly appreciated, Regards.

Hey ChaosGaming,

An object in Unity must always be parented to a Transform, not to a gameObject. So you’d need to change your line into this:

cubeClass.pegObjects[n].transform.parent = cubeClass.cubeObj.transform;

-Patrick

cubeClass.pegObjects[n].transform.parent = cubeClass.cubeObj.transform;

Thanks guys!