- Home /
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
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;
}
}
}
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 ($$anonymous$$R.enabled == false) //if $$anonymous$$R false
{
$$anonymous$$R.enabled = true; //set $$anonymous$$R to true
return; //then logic return and no need to read down anymore
}
$$anonymous$$R.enabled = false; //no need to read by logic
--------------------------------------------------------------but--------------------------------------------------------------
if ($$anonymous$$R.enabled == false) //if $$anonymous$$R not false (logic found not equal false in this step)
{
$$anonymous$$R.enabled = true; //then logic skip this
return; //and skip this too because both of them inside if{ }
}
$$anonymous$$R.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 { }. C$$anonymous$$IIW...................... 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
Follow this Question
Related Questions
Reloading a Scene throws NullReferenceException on GO with nested Prefabs 0 Answers
Any other method to hide and show gameobjects? 1 Answer
Can I hide/show objects by layer or tags? 3 Answers
Toggle Game Objects with specific tag at runtime 0 Answers
How to change one child object to another with the same armature? 0 Answers