- Home /
Switching mesh through Mesh Filter C#
Hi, I'm new to coding, sorry if this is a rudimentary question but I've been having trouble figuring it out based on similar questions I've seen. Be harsh, don't feel shy about telling me where I screwed up. I'm a 3D artist, this is all a new language to me.
I'm building a basic gallery to showcase a few different meshes and I want to build in the ability to switch to a specific mesh from my Assets on a button press while keeping the materials the same. I'm thinking that it's possible to do in C# through the Mesh Filter, but I'm not having any luck.
Here is the code I've cobbled together so far. Any help is hugely appreciated.
using UnityEngine;
using System.Collections;
public class MeshSelect : MonoBehaviour {
void Start () {
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Keypad3))
{
gameObject.GetComponent<MeshFilter>().mesh = Resources.Load("<Mapped/DAFO3");
}
}
}
It would help to know what errors or incorrect behavior you're getting. Without that, all I notice at the moment is that the string passed to Resources.Load doesn't look like a valid filename.
The error I get is
Assets/Scripts/Swap.cs(16,71): error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.$$anonymous$$esh'. An explicit conversion exists (are you missing a cast?)
I know that the filename probably isn't accurate. If I want to reference a mesh in my assets folder, what do I need to input? Do I use a filepath, the name of the .obj, or something else?
Answer by Mikilo · Aug 06, 2015 at 02:22 PM
Hello dude.
Because you are an artist I won't give you precise technical details. When using Resources.Load, give a valid path relative to any Resources folders. In your case replace your line 14 by:
gameObject.GetComponent<MeshFilter>().mesh = Resources.Load<Mesh>("Mapped/DAFO3");
It should work.
I appreciate it, but I'm getting this error now:
Assets/Scripts/Swap.cs(16,44): error CS0411: The type arguments for method `UnityEngine.GameObject.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly
Never$$anonymous$$d, refreshed the page and got the edited version. I think my filepath may still be messed up though. The old mesh disappears, but the new one doesn't appear. If I have my meshes as .objs in Assets/$$anonymous$$apped, am I referencing them correctly?
You need to put your 3D model in a Resources folder, like in "Assets/Resources/$$anonymous$$apper/DAF03.objs". Because the Resources.Load only looks for assets in Resources folders.
Try .obj or .fbx.
Okay, so I need to put my mesh folder into another folder named Resources. Gotcha.
Works like a charm! Thanks a lot $$anonymous$$ikilo!
Glad it works. =D
To clarify for futur developers, please set the thread solved.
Answer by Cascade DAFO · Aug 06, 2015 at 03:16 PM
I came up with a pretty inelegant solution, don't know if anyone has any insight into how I could refine it. I've attached all the meshes to an empty parent GameObject, then attached a script that enables them on a keypress and disables the other meshes. It's not perfect, and I have a slightly different code for each one. Is there a way to refine this code to something that can be applied across all the meshes? It's only a few lines, and it works, but it seems like a messy way of doing things.
using UnityEngine;
using System.Collections;
public class SelectDAFO4 : MonoBehaviour {
public Renderer rend;
void Start () {
rend = GetComponent<Renderer> ();
}
void Update ()
{
//button to enable mesh
if (Input.GetKeyDown (KeyCode.Keypad4)) {
rend.enabled = true;
}
//All other buttons to disable mesh
else if (Input.GetKeyDown (KeyCode.Keypad3)) {
rend.enabled = false;
}
else if (Input.GetKeyDown (KeyCode.Keypad5)) {
rend.enabled = false;
}
}
}
Your answer
