Hi guys, please I really need help with this as I’m just stuck and can’t move forward.
Let me break it down, please bear with me.
Basically there are 4 switches - Switch 1, Switch 2, Switch 3, Switch 4.
2 Gates - Gate 1 and Gate 2
1 Platform
1 Cage
This is the way the logic is supposed to work and the order.
You press Switch 1 with Character 1, and Platform 1 on character 2’s side, starts moving, but a cage locks Character 1 in. So you switch to Character 2. Character 2 can’t get to switch 3 until platform 1 starts moving.
Character 2 presses switch 3 which opens gate 1. Character 1 is still locked in.
After switch 3 is pressed, Character 2 goes to switch 4 which opens the cage.
Then character 2 presses switch 2 which opens gate 2 for character two.
This is the order:
Switch 1, moves platform
Switch 3 opens gate 1
Switch 4 opens cage
Switch 2 opens gate 2
public bool SwitchOne;
public bool SwitchTwo;
public bool SwitchThree;
public bool SwitchFour;
public bool SidePlatformOne;
public bool SidePlatformTwo;
public bool UpDownOne;
public GameObject platform;
// public GameObject gateoneleveltwo;
// public GameObject gatetwoleveltwo;
public GameObject gate;
public GameObject cage;
public bool CageOne;
private Animator anim;
private Animator animone;
private Animator animtwo;
public bool gateone;
public bool gatetwo;
// Use this for initialization
void Start () {
anim = platform.GetComponent<Animator>();
animone = gate.GetComponent<Animator>();
animtwo = cage.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
if (gameObject.tag == "SwitchOne")
{
SwitchOne = true;
SidePlatformTwo = true;
anim.SetTrigger ("SidePlatformTwo");
if((SwitchOne == true) && (SidePlatformTwo == true))
{
CageOne = true;
animtwo.SetTrigger ("GateBlockOne"); //Block's Tarro In and activates her platform
}
}
if(gameObject.tag == "SwitchTwo")
{
SwitchTwo = true;
}
if((SwitchTwo == true) && (gateone == true) && (SwitchThree == true))
{
gatetwo = true;
animone.SetTrigger ("GateTwoLevelTwo");
}
if(gameObject.tag == "SwitchThree")
{
SwitchThree = true;
}
if((SwitchThree == true) && (CageOne == true))
{
gateone = true;
animone.SetTrigger ("GateOneLevelTwo");
}
if(gameObject.tag == "SwitchFour")
{
SwitchFour = true;
}
if((SwitchFour == true) && (SwitchThree == true))
{
CageOne = false;
}
}
}
The code works fine until I press switch three which does nothing. Pressing switch 3 is supposed to open gate one as long as switch 1 is pressed.
Let’s start by eliminating silliness in the existing code… like when you set something, and then immediately check if it’s set. (Meaning no offense here; we all write silly code sometimes.) Also the indentation is wrong; in several places it suggests some sort of dependency where there isn’t one. So, here’s a modification of your code you might try; at least it should be slightly clearer.
public bool SwitchOne;
public bool SwitchTwo;
public bool SwitchThree;
public bool SwitchFour;
public bool SidePlatformOne;
public bool SidePlatformTwo;
public bool UpDownOne;
public GameObject platform;
// public GameObject gateoneleveltwo;
// public GameObject gatetwoleveltwo;
public GameObject gate;
public GameObject cage;
public bool CageOne;
private Animator anim;
private Animator animone;
private Animator animtwo;
public bool gateone;
public bool gatetwo;
// Use this for initialization
void Start () {
anim = platform.GetComponent<Animator>();
animone = gate.GetComponent<Animator>();
animtwo = cage.GetComponent<Animator>();
}
void OnTriggerEnter (Collider other)
{
Debug.Log("Collided with: " + gameObject.tag); // important debugging aid!
if (gameObject.tag == "SwitchOne")
{
SwitchOne = true;
SidePlatformTwo = true;
anim.SetTrigger ("SidePlatformTwo");
CageOne = true;
animtwo.SetTrigger ("GateBlockOne"); //Blocks Tarro In and activates her platform
}
if(gameObject.tag == "SwitchTwo")
{
SwitchTwo = true;
}
if((SwitchTwo == true) && (gateone == true) && (SwitchThree == true))
{
gatetwo = true;
animone.SetTrigger ("GateTwoLevelTwo");
}
if(gameObject.tag == "SwitchThree")
{
SwitchThree = true;
}
if((SwitchThree == true) && (CageOne == true))
{
gateone = true;
animone.SetTrigger ("GateOneLevelTwo");
}
if(gameObject.tag == "SwitchFour")
{
SwitchFour = true;
}
if((SwitchFour == true) && (SwitchThree == true))
{
CageOne = false;
}
}
}
But AndreasU is right; if the DebugLog in the “if (gameObject.tag == “SwitchThree”)” case didn’t appear, then the problem isn’t your logic at all; the trigger is simply not firing, or the game object is not correctly tagged. I added a DebugLog right at the top of the method that should make it clearer what’s going on.
First of thank you to both JoeStrout and AndreasU, thanks for trying to help. I just started using Unity 3 months ago and I’m still a beginner so believe me no offense taken. I love the feedback.
Now JoeStrout thanks for the revision. When I collide with each of the switches, the debug shows that the switch hit, but the gates didn’t open. I checked the parameters again and tags just to be sure and nothing worked.
OK, if the gates don’t open there are several places it could go wrong — you just need to check and eliminate each one until you find the culprit:
The OnTriggerEnter function isn’t being called. Sounds like you’ve already eliminated this one.
The OnTriggerEnter function isn’t going to the right place in the code. An additional DebugLog inside the “if” block that you expect to execute can check this one.
The animation controller trigger isn’t being set. Hard to believe if you’ve eliminated the previous possibility, but easy to check: just open up the animation controller window while playing the game, and watch the triggers.
The triggers are being set, but your animation controller isn’t using them. This means some flaw not in your code, but in the animation controller state machine. Again, should be pretty obvious if you watch the state machine while testing your game. Also you can manually set the triggers right in the Animation Controller window, and see whether everything behaves (gates open etc.) as you expect.
There’s a lot of plumbing here, but if you trace it through step by step, I bet you can find where it’s broken!
Ok. I’ve checked everything, added debug log. The code that executes the gate open logic doesn’t work. The gate is supposed to open as soon as switch 1 and switch 3 are pressed. But it doesn’t work.
To check if the animation is working for the gates, I used switch one and the animation parameters for both gates and they worked fine. The gates opened.
Switch three is being hit, but gate one doesn’t open. it doesn’t run the animation trigger code.
I’m not too sure by what you mean by this.
The OnTriggerEnter function isn’t going to the right place in the code. An additional Debug Log inside the “if” block that you expect to execute can check this one.
But the ontriggerenter works as all the switches work as shown in the debug log.
I’m using one script for 4 gameobject switches and colliders. Maybe my setup is wrong there I don’t know.
I know you added something like this before, but let’s try to be thorough and step-by-step, just to be sure. When you add this and test, it seems to me there are only two possibilities:
The message “Setting the ‘GateOneLevelTwo’ trigger” appears. In this case, the logic of this code is correct, but setting that trigger doesn’t actually do what you think it does.
The message does not appear. In that case, somehow, the if statement’s condition is not true… and since we just set SwitchThree to true here, it must be that CageOne is no longer true.
Either result is progress, so please report back what you find!
Hmm. Well the easiest way is to broadcast a message that all the switches listen for. You can do this with BroadcastMessage. But you’ll have to put all the switches inside a common parent in the hierarchy, and then call BroadcastMessage on that parent. It will then pass it down to all the children (i.e., the switch objects).