- Home /
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();
You can instantiate the quad as a GameObject and use gridPart.gameObject.transform; I didn't get the rest, just pointing out you don't have to use Transform just to access parent/child
Answer by NoseKills · Apr 19, 2014 at 01:28 AM
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;
}
}
Hope you don't $$anonymous$$d, I converted your comment to an Answer.
Well this isn't quite doing it for me, though it answers the question in part. The object has to be a transform because it can't be instantiated as a gameobject (for some reason).
"Can't cast from source type to destination type"
I should have mentioned that originally.
And I wrote the SetTexture() method as a way of shortening the code.
I guess the "squarePrefab" is of type Transform then? I'll change my answer a bit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding a prefab to gameObject at a certain position in runtime 0 Answers
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer
move a GameObject based on Input.GetTouch 1 Answer
Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 0 Answers