- Home /
not working- GameObject.GetComponent(MeshFilter).mesh;
EDIT-it seems I was testing on some crudely imported prefabs that have a visible mesh but they don' t have the mesh-filter ar a mesh-renderer. SORRY!
original question-I need to get the mesh bounds (i.e. real size) of an instantiated object.
it returns null reference exception when I ask for GameObject.GetComponent(MeshFilter).mesh; after instantiating it. I can Instantiate and rescale the object but I can' t get it' s Mesh.
Here is the code
for (var aa:int = 0; aa < 9; aa ++){
var r : Quaternion; r.eulerAngles = Vector3.zero;
var k = Camera.main.ViewportToWorldPoint (Vector3 (aa*.1+.1,.05, 1));
var prefabline = Instantiate(myprefabs[aa], k, r);
prefabline.transform.LookAt(Camera.main.transform.position);
var mesh : Mesh = prefabline.GetComponent(MeshFilter).mesh;
var bounds : Bounds = mesh.bounds;
prefabline.transform.localScale = prefabline.transform.localScale*.1/bounds.size.x;
}
perhaps the Mesh has not been instantiated so I need a yield instruction? is it possible for me to get the Mesh of a Prefab? I already tried:
var mesh : Mesh = myprefabs[aa].GetComponent(MeshFilter).mesh;
Answer by lil_billy · Nov 19, 2012 at 05:36 PM
hold why do you have the .mesh extension after using GetComponent that shouldnt be there as far as I know and ive done things like this before
oh and as a type you said Mesh but in GetComponent you MeshFilter choose which type you are getting a Mesh or MeshFilter
simply try var mesh : MeshFilter = prefabline.GetComponent(MeshFilter);
sorry i keep having to edit this
Thanks! I did try that and it said Cannot convert 'UnityEngine.$$anonymous$$eshFilter' to 'UnityEngine.$$anonymous$$esh'. I think there is a problem with my prefabs, I am testing it on some crudely imported prefabs from Google warehouse that don' t have a mesh filter! Silly me! They have a mesh but not a mesh filter it' s strange. I will figure a solution now!
glad i could help $$anonymous$$d giving me a rating- I keep answering people successfully but they dont rate, its killing my acceptance
I can' t really rate the answer because it' s incorrect,so I tried to find some other answers you gave and mark them up for the effort!I found on-I marked up your debug question because it' s a great question.
wait i thought I answered this question, is there something more you were looking for in the answer, what is incorrect?
Your answer
Follow this Question
Related Questions
Instantiate within boundaries of Mesh shape 2 Answers
How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers
Getting the mesh component of an object 1 Answer
Is the bounds.size and the GetComponent.BoxCollider2D.size of a GameObject meant to be the same? 1 Answer
Mirroring a mesh? 1 Answer