• 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 4ndik4w · May 14, 2017 at 07:00 PM · gameobjecthidemeshrenderershow

hide and show node for turret places

hello guys, i got a problem here to hide and show node. when first play to test code, all node always hide perfectly. then i try hit turret button to select turret and show all node, the problem come. the node just three of them has appear in game. currectly i have 36 node in game.

here my code:

 using UnityEngine;
     
     
 public class Shop : MonoBehaviour
 {
         public GameObject nodes;
         private MeshRenderer[] meshRend;
     
         public TurretBlueprint gutlingPro;
     
         BuildManager buildManager;
     
     
         private void Start()
         {
             buildManager = BuildManager.instance;
             meshRend = nodes.GetComponentsInChildren<MeshRenderer>();
             EnableDisableNodeMeshRenderer(); //set node mesh render to false when first start game
         }
     
     
         public void SelectGatlingPro()
         {
             Debug.Log("GutlingPro Selected");
             buildManager.SelectTurretToBuild(gutlingPro);
             EnableDisableNodeMeshRenderer(); //set node mesh render to true when turret button clicked
         }
     
     
         public void EnableDisableNodeMeshRenderer()
         {
             if (meshRend != null)
             {
                 foreach (MeshRenderer MR in meshRend)
                 {
                     if (MR.enabled == false)
                     {
                         MR.enabled = true;
                         return;
                     }
     
                     MR.enabled = false;
                 }
             }
         }
 }



and all three node that appear is three first node in child.......... i have GO name Nodes for parent and default unity cube GO name Node for child so, what is going wrong in my code? please guys, i need your help, im stuck and confused.

Note: no error and warning in VS error list, no error and warning too in Unity Console

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

1 Reply

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

Answer by slavo · May 15, 2017 at 12:54 PM

Hello,

you probably want use "continue" instead of "return" in your loop. Return immidietly terminate loop, but continue jump to next element.

       public void EnableDisableNodeMeshRenderer()
          {
              if (meshRend != null)
              {
                  foreach (MeshRenderer MR in meshRend)
                  {
                      if (MR.enabled == false)
                      {
                          MR.enabled = true;
                          continue;   //was return
                      }
      
                      MR.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 4ndik4w · May 15, 2017 at 03:37 PM 0
Share

thank you so much guy, and if you have more time, can you explain what happen and why just change return; to continue; become work like i want.......

here is my opinoin on C# logic work:

   if (MR.enabled == false) //if MR false
   {
          MR.enabled = true; //set MR to true
          return; //then logic return and no need to read down anymore
   }
           
   MR.enabled = false; //no need to read by logic

--------------------------------------------------------------but--------------------------------------------------------------

   if (MR.enabled == false) //if MR not false (logic found not equal false in this step)
   {
         MR.enabled = true; //then logic skip this
         return; //and skip this too because both of them inside if{ }
   }
           
    MR.enabled = false; //then logic read outside if { } and found this code then set to false because logic see if up there is not ==false

simple is : when if statement meet, the logic read inside if { }............ when if statement not meet, the logic skip everything inside if { } and read next code inside foreach { }. CMIIW...................... XD

i just want to know more about logic code work........... because before test it code, i think the logic work like my ilustration above, and i think that is logic but actually not after test the code. just wonder why................

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I hide/show objects by layer or tags? 3 Answers

Any other method to hide and show gameobjects? 1 Answer

Toggle Game Objects with specific tag at runtime 0 Answers

help hiding object? 2 Answers

Hide but no deactivate a gameobject 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges