1
\$\begingroup\$

So in working with my game, there aren't any syntax or compile errors, but there's this one bug that I've been working on to fix for a few days on and off and I can't seem to make heads or tails of the reason why it doesn't work, let me explain.

console layout

This is what the player sees when they start out, and their actions are to be able to click on two wires on the same side (right-right and left-left) and the should both switch positions. first click

In this image,the blue wire was the first one clicked, and the highlight/highlightDirection variables I have set are assigned to 'w2' (the blue wire) and the 'topLeft' direction for the purpose of changing the wire's sprite correctly when it changes positions. second click

And now I've clicked on the yellow wire, setting the changelight/changelightDirection to 'w3' (the yellow wire) and the 'middleLeft' direction for the purpose of changing the wire's sprite correctly when it changes positions, and after changelight/changelightDirection have been assigned, the wires shoudl both switch positions, but instead, this happens. wire switch

Only the yellow wire (The changelight/changeligthDirection wire) switched sprites, and this has been consistent throughout all of the combinations possible.

To try and fix this, I've gone over the script for the TopLeft and MiddleLeft scripts dozens of times, yet for some reason the same problem persists, and only the second clicked wire changes positions TopLeft.cs

    void OnMouseDown () {
    if (levelManager.highlight == "none") {
        levelManager.highlight = levelManager.leftTopWire;
        levelManager.highlightDirection="topLeft";
    } else { 
        levelManager.changelight = levelManager.leftTopWire;
        levelManager.changelightDirection = "topLeft";
        if (levelManager.highlight == "w1" || levelManager.changelight=="w1") {
            wireOne.SpriteChange ();
        }
        if (levelManager.highlight == "w2" || levelManager.changelight=="w2") {
            wireTwo.SpriteChange ();
        }
        if (levelManager.highlight == "w3" || levelManager.changelight=="w3") {
            wireThree.SpriteChange ();
        }
    }
}

MiddleLeft.cs

    void OnMouseDown () {
    if (levelManager.highlight == "none") {
        levelManager.highlight = levelManager.leftMiddleWire;
        levelManager.highlightDirection="middleLeft";
    } else {
        levelManager.changelight = levelManager.leftMiddleWire;
        levelManager.changelightDirection = "middleLeft";
        if (levelManager.highlight == "w1" || levelManager.changelight=="w1") {
            wireOne.SpriteChange ();
        }
        if (levelManager.highlight == "w2" || levelManager.changelight=="w2") {
            wireTwo.SpriteChange ();
        }
        if (levelManager.highlight == "w3" || levelManager.changelight=="w3") {
            wireThree.SpriteChange ();
        }
    }
}

In the code for each scripts, no matter if the that wire is assigned to either changelight or highlight, then that wire's script will run it's SpriteChange function, unless I'm missing something that's how the code should work, and suggestions on how to fix this?

EDIT: Here's the code for the SpriteChange() functions for scripts WireOne.cs, WireTwo.cs, WireThree.cs

public void SpriteChange(){
    if (levelManager.changelight == "w1") {
        if (levelManager.highlightDirection == "middleLeft" || levelManager.highlightDirection == "bottomLeft" || levelManager.highlightDirection == "topLeft") {
            if (levelManager.highlightDirection == "middleLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTop;

            }
            if (levelManager.highlightDirection == "middleLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightMiddle;

            }
            if (levelManager.highlightDirection == "middleLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownBottom;

            }
            if (levelManager.highlightDirection == "topLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightTop;

            }
            if (levelManager.highlightDirection == "topLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTop;

            }
            if (levelManager.highlightDirection == "topLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTall;

            }
            if (levelManager.highlightDirection == "bottomeLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTall;

            }
            if (levelManager.highlightDirection == "bottomLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpBottom;

            }
            if (levelManager.highlightDirection == "bottomLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightBottom;


            }

        } else {//This is if the highlight direction is from the right
            if (levelManager.highlightDirection == "middleRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTop;

            }
            if (levelManager.highlightDirection == "middleRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightMiddle;

            }
            if (levelManager.highlightDirection == "middleRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpBottom;

            }
            if (levelManager.highlightDirection == "topRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightTop;

            }
            if (levelManager.highlightDirection == "topRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTop;

            }
            if (levelManager.highlightDirection == "topRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTall;

            }
            if (levelManager.highlightDirection == "bottomeRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTall;

            }
            if (levelManager.highlightDirection == "bottomRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownBottom;

            }
            if (levelManager.highlightDirection == "bottomRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightBottom;

            }

        }
    }
    else{ //This is if the wire is in the highlight instead of the changelight
        if (levelManager.changelightDirection == "middleLeft" || levelManager.changelightDirection == "bottomLeft" || levelManager.changelightDirection == "topLeft") {
            if (levelManager.changelightDirection == "middleLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTop;

            }
            if (levelManager.changelightDirection == "middleLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightMiddle;

            }
            if (levelManager.changelightDirection == "middleLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownBottom;

            }
            if (levelManager.changelightDirection == "topLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightTop;

            }
            if (levelManager.changelightDirection == "topLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTop;

            }
            if (levelManager.changelightDirection == "topLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTall;

            }
            if (levelManager.changelightDirection == "bottomeLeft" && rightPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTall;

            }
            if (levelManager.changelightDirection == "bottomLeft" && rightPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpBottom;

            }
            if (levelManager.changelightDirection == "bottomLeft" && rightPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightBottom;

            }

        } else {//This is if the changelight direction is from the right
            if (levelManager.changelightDirection == "middleRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTop;

            }
            if (levelManager.changelightDirection == "middleRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightMiddle;

            }
            if (levelManager.changelightDirection == "middleRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpBottom;

            }
            if (levelManager.changelightDirection == "topRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightTop;

            }
            if (levelManager.changelightDirection == "topRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTop;

            }
            if (levelManager.changelightDirection == "topRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = downUpTall;

            }
            if (levelManager.changelightDirection == "bottomeRight" && leftPos == "Top") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownTall;

            }
            if (levelManager.changelightDirection == "bottomRight" && leftPos == "Middle") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = upDownBottom;

            }
            if (levelManager.changelightDirection == "bottomRight" && leftPos == "Bottom") {
                gameObject.GetComponent<SpriteRenderer> ().sprite = straightBottom;


            }
        }
    }
    levelManager.highlight = "none";
    levelManager.highlightDirection = "none";
    levelManager.changelight = "none";
    levelManager.changelightDirection = "none";
}
\$\endgroup\$
4
  • \$\begingroup\$ What is your SpriteChange() method? Does it change levelManager.highlight or levelManager.changelight? \$\endgroup\$ – Vaillancourt Jun 29 '15 at 23:43
  • \$\begingroup\$ it actually changes changes the wire's sprite and position based on levelManager.highlight, levelManager.highlightDirection, levelManager.changelight, and levelManager.changeDirection, I could post the code for SpriteChange() if it would help. \$\endgroup\$ – LevelForge Jun 30 '15 at 1:16
  • \$\begingroup\$ Yes, please update the question with the code as it may clear up some things! \$\endgroup\$ – Vaillancourt Jun 30 '15 at 2:13
  • \$\begingroup\$ Sorry, should have done that from the start to be honest, hope this helps. \$\endgroup\$ – LevelForge Jun 30 '15 at 2:24
1
\$\begingroup\$

That one is easy and, yes, even after many years as a programmer I stumble upon this kind of situation.

In SpriteChange you change values held by your levelManager:

levelManager.highlight = "none";
levelManager.highlightDirection = "none";
levelManager.changelight = "none";
levelManager.changelightDirection = "none";

However, in your else parts of your two first scripts you rely on the values of levelManager.changelight or levelManager.highlight to be something else than "none". This is why only the first part is done: when it is ready to go for a second time (evaluate to true in a second if), all the important values are set to "none", which causes the condition checks to result to false.

To fix your issue, I would suggest to place the code that resets your values after the three ifs in each of your else parts of your OnMouseDown methods instead of in your SpriteChange methods:

TopLeft.cs

void OnMouseDown () {
    if (levelManager.highlight == "none") {
        levelManager.highlight = levelManager.leftTopWire;
        levelManager.highlightDirection="topLeft";
    } else { 
        levelManager.changelight = levelManager.leftTopWire;
        levelManager.changelightDirection = "topLeft";
        if (levelManager.highlight == "w1" || levelManager.changelight=="w1") {
            // If you got here because (levelManager.highlight == "w1") == true,
            // it means that levelManager.changelight == _something else_ so you
            // **should not** reset its value in wireOne.SpriteChange ()
            // so that it can be evaluated to true in the other ifs. 
            wireOne.SpriteChange ();
        }
        if (levelManager.highlight == "w2" || levelManager.changelight=="w2") {
            wireTwo.SpriteChange ();
        }
        if (levelManager.highlight == "w3" || levelManager.changelight=="w3") {
            wireThree.SpriteChange ();
        }
        // You no longer need these at this point, you can reset them. 
        levelManager.highlight = "none";
        levelManager.highlightDirection = "none";
        levelManager.changelight = "none";
        levelManager.changelightDirection = "none";
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Oh my god, that totally worked, whenever I went over my code, I never even considered that segment because I considered it not connected to the issue, thank you so much Alexandre. \$\endgroup\$ – LevelForge Jun 30 '15 at 3:36
  • \$\begingroup\$ @LevelForge I'm glad I could help! \$\endgroup\$ – Vaillancourt Jun 30 '15 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.