- Home /
How to access mesh from Scriptable Object directly?
I have scriptable object class as flows:
public class ItemObjectt : ScriptableObject
public GameObject prefab; // let's say linked prefab is Flower
I'm trying to access mesh from this "Flower" from another script and assign in editor. I can create public mesh and it will work(... = item.mesh). But wondering if I can simplify this somehow
I would assume it will look something like this:
...
public ItemObjectt item;
GetComponentInChildren<MeshFilter>().mesh = item.GetComponent(typeof(MeshFilter));
and
GetComponent<MeshCollider>().sharedMesh = item.GetComponent(typeof(MeshFilter));
Answer by GMRM · Feb 17 at 10:28 PM
I think I figured out this one. Works as I wanted. Not sure if it is the best way to code these things for the Editor =)
public ItemObjectt item;
var prefabmesh = item.prefab.GetComponent<MeshFilter>().sharedMesh;
var prefabrend = item.prefab.GetComponent<Renderer>().sharedMaterial;
GetComponentInChildren <Renderer>().sharedMaterial = prefabrend;
EditorUtility.SetDirty(GetComponentInChildren<Renderer>());
GetComponentInChildren<MeshFilter>().mesh = prefabmesh;
EditorUtility.SetDirty(GetComponentInChildren<MeshFilter>());
GetComponent<MeshCollider>().sharedMesh = prefabmesh;
EditorUtility.SetDirty(GetComponent<MeshCollider>());
Your answer
Follow this Question
Related Questions
MeshCollider (concave) doesn't trigger OnCollision functions 0 Answers
Why aren't the mesh collider and mesh filter meshes saved in prefab children? 2 Answers
I am trying to deform a mesh (Plane) imported from Blender 1 Answer
Assigning SharedMesh is creating an unreferenced instance 0 Answers
Skill system 0 Answers