- Home /
Enabling MeshRenderers not working
Hey there! I usually never go to the forums for a problem I have created. I usually just stay for hours, and fix it eventually. But here, I'm stuck. I have been trying for days and still haven't been able to find a solution. Here is my code:
public void UpdateWeaponSlot1() {
//check equipment slot 1 for a weapon
for(int i = 0; weaponGOs.Length > i; i++) {
//if equipment slot 1 is empty, catch the NullReferenceException
try {
//if active, enable the MeshRenderers
if (inventory.equipmentPanel.equipmentSlots[0].Item.ItemName == weaponGOs[i].name) {
//enable the MeshRenderer on the GameObject
weaponGOs[i].GetComponent<MeshRenderer>().enabled = true;
//enable the MeshRenderers of the children from the GameObject
for(int e = 0; weaponGOs[i].GetComponentsInChildren<MeshRenderer>().Length > e; e++) {
weaponGOs[i].GetComponentsInChildren<MeshRenderer>()[e].enabled = true;
}
}
} catch {
weaponGOs[i].GetComponent<MeshRenderer>().enabled = false;
for (int e = 0; weaponGOs[i].GetComponentsInChildren<MeshRenderer>().Length > e; e++) {
weaponGOs[i].GetComponentsInChildren<MeshRenderer>()[e].enabled = false;
}
}
}
}
public void UpdateWeaponSlot2() {
//check equipment slot 2 for a weapon
for (int i = 0; weaponGOs.Length > i; i++) {
//if equipment slot 2 is empty, catch the NullReferenceException
try {
//if active, enable the MeshRenderers
if (inventory.equipmentPanel.equipmentSlots[1].Item.ItemName == weaponGOs[i].name) {
//enable the MeshRenderer on the GameObject
weaponGOs[i].GetComponent<MeshRenderer>().enabled = true;
//enable the MeshRenderers of the children from the GameObject
for (int e = 0; weaponGOs[i].GetComponentsInChildren<MeshRenderer>().Length > e; e++) {
weaponGOs[i].GetComponentsInChildren<MeshRenderer>()[e].enabled = true;
}
}
} catch {
weaponGOs[i].GetComponent<MeshRenderer>().enabled = false;
for (int e = 0; weaponGOs[i].GetComponentsInChildren<MeshRenderer>().Length > e; e++) {
weaponGOs[i].GetComponentsInChildren<MeshRenderer>()[e].enabled = false;
}
}
}
}
This is running in Update() (So that is it easier to post here and for troubleshooting purposes), what this is supposed to do, is check a certain equipment slot for every weapon in the game. If one found, it should enable the MeshRenderers so that you can see that the player has the weapon. What happens is, if I first pick up a weapon for slot 2, and then for slot 1, everything works. However, if I pick up a weapon from slot 1, nothing happens... until I pick up a weapon for slot 2, then everything shows up. Why am I using for loops you may ask? Because if I add more weapons in the future (I will), than the same code will work for all of them. Just to clarify, the problem is that if I pick up a weapon from slot 1, it should show up, doesn't matter if I have a weapon in slot 2 or not. If anyone can help me out here, I will be SO SO SO greatful! Thank you in advance.
Ok, it looks like a few dozens (if not hundreds) of questions have been answered before $$anonymous$$e. Now, I don't want to be greedy or anything, but even telling me that the code I wrote should work will help me a lot! Thank you again!