• 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
1
Question by Brackman · May 15, 2013 at 09:08 PM · gameobjectmeshrenderervisibility

c#: Best practice to toggle visiblity of meshes within a gameObject?

I'm brand new to Unity and have read through tutorials but have not 'clicked' yet with best practices for manipulating the components of a gameObject.

I have exported three meshes from a max file. Mesh01, Mesh02, Mesh03

I wrote a simple c# script that currently contains information with regards to the individual meshes (Mesh class), but labelled them: Healthy, Damaged, Wreck and then plug in the meshes into the vars. I then have a health var and, for the purposes of testing will slowly reduce till it reaches zero.

My problem is that I am unsure how to control the "Mesh Renderer" for each mesh.

Healthy.enabled and Healthy.renderer do not appear to exist at the mesh level. Any pointers would be helpful :).

Thanks,

J.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CodeArtist_MX · May 15, 2013 at 10:09 PM

Ok, so i think i get what your problem is... First of all if you only want a GameObject which contains your model so you can add scripts to it i think you should start by using .fbx files instead of max, since they are more simple to manipulate in unity (unity will create a prefab of that model with all you need and you only have to drag and drop it to the scene, it will render automatically). I Hope this link is helpful:

However according to what i understand you created a script that holds a public "Mesh" type variable and you are trying your .max model into it. There are 2 important things about this:

  • Any script that you are planning to use in the scene must be attatched to a GameObject , you can create a empty one and attatch the script you created into it. (Your script should extend from Monobehavior if you want to use the GameObject properties like ".active", that disables the gameObject completely from the scene)

  • The gameobject that holds the script must have attatched a "Mesh renderer" component in order to render a mesh, you can attatch it from Component->Mesh->MeshRender , also as "whydoidoit" said in another answer you will need a MeshFilter which you can add to your gameObject in Component->Mesh->MeshFilter

You can Enable/Disable the meshfilter like:

 gameObject.GetComponent<MeshRenderer>().enabled=false;

After that im a little bit lost to be honest, since i used this approch to dinamycally make meshes, however if you case you have to load your model by hand using Resources.Load (and according to what i think it should be automatically displayed after that).

This second approach will complicate your workflow a lot and also will make you unable to visualize your model while rendering the scene, since you have to Run your script to Load it.

I would stick to using the .fbx files and drag and dropping the prefab :)

I hope i answered you question and that it helps you in your project!

Comment
Add comment · Show 3 · 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 whydoidoit · May 15, 2013 at 10:45 PM 1
Share

GameObject doesn't have an enabled property - it has activeInHierarchy and activeSelf. Only things derived from Behaviour (and a few other components) have an enabled property.

avatar image CodeArtist_MX · May 15, 2013 at 10:52 PM 0
Share

Thanks! sorry for the mistake, you are completely right, i will fix my answer.

avatar image Brackman · May 16, 2013 at 09:50 PM 0
Share

First off, thanks for the replies. However, I'm still unsure about implementation.

To clarify:

  1. I export the max file as FBX -- and the result in unity is a prefab that contains three meshes with a simple shader/material.

  2. In Unity, a script allows the user to specify the meshes as being a particular type at the top of the prefab, such as Healthy or Damaged or Wrecked.

  3. There is a variable -- lets call it health, that counts down slowly so that on update I can trigger changes based on its value.

The user, for the moment, goes in and sets the health they want to give to the prefab, then they slot in the meshes they want to use for Healthy, Damaged, and Wrecked. When play is pressed the update function checks the health and depending on its value will show the appropriate mesh.

Currently the following works to update the mesh, but I'm concerned about iterating through all the mesh children. This seems inefficient:

 void update$$anonymous$$eshState(string meshname)
 {
 foreach(Transform temp in transform)
 {
 if(temp.name == meshname)
 temp.renderer.enabled = true;
 else
 temp.renderer.enabled = false;
 }
 }

I've attempted to use the $$anonymous$$eshRenderer and the $$anonymous$$eshFilter -- but with no luck so far. This function I've provided seems costly since I'm having to iterate through all meshes -- I'd rather just be specific and use that $$anonymous$$eshFilter idea.

avatar image
0

Answer by whydoidoit · May 15, 2013 at 10:46 PM

A MeshRenderer is attached to a GameObject and the mesh it renders is controlled by a MeshFilter that you add to the same GameObject. You can set the mesh of the MeshFilter to cause the MeshRenderer to show a different mesh.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turning off renderer in other game object (C#) 2 Answers

Can't remove component. MeshRenderer depends on it 1 Answer

appear/disappear gameobject 3 Answers

Game object with animations 0 Answers

Can't render GameObject 3 Answers


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