Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cascade DAFO · Aug 05, 2015 at 03:48 PM · c#meshmeshfilter

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");
         }
     
     }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zach-r-d · Aug 05, 2015 at 07:19 PM 0
Share

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.

avatar image Cascade DAFO · Aug 06, 2015 at 02:18 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cascade DAFO · Aug 06, 2015 at 02:28 PM 0
Share

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

avatar image Cascade DAFO · Aug 06, 2015 at 02:32 PM 0
Share

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?

avatar image Mikilo · Aug 06, 2015 at 03:21 PM 1
Share

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.

avatar image Cascade DAFO · Aug 06, 2015 at 04:02 PM 0
Share

Okay, so I need to put my mesh folder into another folder named Resources. Gotcha.

Works like a charm! Thanks a lot $$anonymous$$ikilo!

avatar image Mikilo · Aug 06, 2015 at 04:08 PM 0
Share

Glad it works. =D

To clarify for futur developers, please set the thread solved.

avatar image
0

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;
         } 
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Aug 31, 2015 at 11:15 AM 0
Share

I didnt understand your question...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C#, Creating a single mesh polygon problems 2 Answers

Lighting issues with script-generated polygons 1 Answer

Procedural array of meshes problem / solved 1 Answer

How do i modify a mesh Filter(C#)? 2 Answers

Get all vertices in gameObject to array. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges