I've got WallCreator script to place walls in Unity, and another one, WallCreatorSwitcher to turn WallCreator ON/OFF by checking the toggle. I also wanted to change wallPrefab's layer there by using GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("LayerName");, so when the toggle is checked (WallCreator ON) layer is "Ignore Raycast" - and it works, and if the toggle is unchecked (WallCreator OFF) layer is "Default" - but the problem is, it's not changing the layer in this case.
public class WallCreatorSwitcher : MonoBehaviour {
public Toggle toggle;
public Text text;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (toggle.isOn)
{
GetComponent<WallCreator>().enabled = true;
Debug.Log("Wall Creator is ON");
text.enabled = true;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Ignore Raycast");
}
else
{
GetComponent<WallCreator>().enabled = false;
Debug.Log("Wall Creator is OFF");
text.enabled = false;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Default");
}
}
}
GetComponent<WallCreator>().enabled = false;stops the component being interacted with at all, as a result you may have to turn it on, change the layer, and then turn it back off, alternatively make the layer switch before switching it off - Alfie GoodacreGetComponent<WallCreator>().enabled = false;(so I'm able to place walls even when the toggle is unchecked) the layer is changing only for new objects. I mean, for example, I place wall1 with toggle ON - it's layer is "Ignore Raycast", then i switch toggle to OFF, place wall2 - it's layer is "Default", but the layer of wall1 doesn't change - artur47wienwall):GameObject[] walls = GameObject.FindGameObjectsWithTag("wall");then you may manipulate them through a foreach loop - Alfie GoodacreWallCreatorscript and describe where each script is attached - Programmer