Access method of an instance

I have an instance of a quad that has a method SetTexture(). I can instantiate the quad, but it has to be a transform so that I can set it as a child. But I still need to be able to access quad.SetTexture() so that I don’t have to have a different prefab for every texture. Commented lines don’t work.

//squarePrefab.SetTexture();
						//squarePrefab.transform.SetTexture();
						//squarePrefab.gameObject.SetTexture();
						//squarePrefab.transform.gameObject.SetTexture();
						Transform gridPart = (Transform) Instantiate(squarePrefab, pos, Quaternion.identity);
						gridPart.transform.parent= transform;
						//gridPart.gameObject.SetTexture();
						//gridPart.SetTexture();
						//(GameObject) gridPart.SetTexture();
						//Square gridPart = Square("Texture/Terrain/dirt0");
						//Transform instance = (Transform) Instantiate(gridPart);
						//GameObject gridPart = (GameObject) Instantiate(squarePrefab, pos, Quaternion.identity);
						//gridPart.SetTexture();

In order to understand what’s happening and how to fix this, you need to understand the way the building blocks of unity games are constructed.

A GameObject or a Transform does not have a function called SetTexture(). A GameObject can have a component attached to it and this component can have a method called SetTexture().

How do you know there is a method called SetTexture()?
If you added a script to the prefab and the script has that method, you should do:

GameObject gridPart  = (GameObject)GameObject.Instantiate(squarePrefab);
		gridPart.transform.parent = transform;
		
		YourScriptClassName script = gridPart.GetComponent<YourScriptClassName>();
		script.SetTexture();

But if you’re for example talking about the SetTexture method of the Material class, you should:

GameObject gridPart  = (GameObject)GameObject.Instantiate(squarePrefab);
		gridPart.transform.parent = transform;

		MeshRenderer mr = gridPart .GetComponent<MeshRenderer>();
		mr.material.SetTexture("_MainTex", yourTextureVariable);

You can mix and match but you have to stay true to Types in your code:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	public GameObject squarePrefabGameObject;
	public GameObject gridPartGameObject; 

	public Transform squarePrefabTransform;
	public Transform gridPartTransform; 

	void functionX () 
	{
		//Option 1
		// If the prefabs are declared as GameObjects, you have to stay true to the type...
		gridPartGameObject  = (GameObject)GameObject.Instantiate(squarePrefabGameObject);
		// you can still access the transfrom easily...
		gridPartGameObject.transform.parent = transform;

		MeshRenderer mr = gridPartGameObject.GetComponent<MeshRenderer>();
		mr.material.SetTexture("_MainTex", _Texture);

		// if you added a script with yur own functions to the prefab, you need to get the script first...
		YourScriptClassName script = gridPartGameObject.GetComponent<YourScriptClassName>();
		script.SetTexture();
	}

	void functionY()
	{
		// Option 2
		// If the prefabs are declared as Transforms...
		gridPartTransform  = (Transform)GameObject.Instantiate(squarePrefabTransform);
		gridPartTransform.parent = transform;
		
		MeshRenderer mr = gridPartTransform.GetComponent<MeshRenderer>();
		mr.material.SetTexture("_MainTex", _Texture);
		
		YourScriptClassName script = gridPartTransform.GetComponent<YourScriptClassName>();
		script.SetTexture();



		// a "transform" has a reference to "gameObject" and vice versa, so whichever your prefabs are declared as
		// you can access one via the other
		GameObject instantiatedGameObject = (GameObject)GameObject.Instantiate(squarePrefabGameObject);

		// a GameObject has a transform that describes its orientation and position 
		gridPartTransform = instantiatedGameObject.transform;

		// a transform is connected to a GameObject
		gridPartGameObject = gridPartTransform.gameObject;
	}


}