So I have a switch that controls platforms that move up and down. The code looks like this:
using UnityEngine;
using System.Collections;
public class ElevatorSwitchScript : MonoBehaviour
{
public bool isSwitched=false;
public float platformSpeed=5;
public Transform elevator;
public Vector3 desiredPos = new Vector3(0f, 0f, 0f);
private Vector3 originalPos;
public GameManager gameManager;
void Start ()
{
originalPos=elevator.transform.position;
}
void OnTriggerEnter (Collider otherObject)
{
isSwitched=true;
}
void Update()
{
if (gameManager.resetSwitch==true)
{
isSwitched=false;
gameManager.resetSwitch=false;
}
if (isSwitched==false)
{
elevator.transform.position=originalPos;
renderer.material.color=Color.red;
}
if (isSwitched==true)
{
renderer.material.color=Color.green;
if (elevator.transform.position.y <= desiredPos.y)
elevator.transform.Translate (Vector3.up * platformSpeed * Time.deltaTime);
if (elevator.transform.position.y >= desiredPos.y)
elevator.transform.Translate (Vector3.down * platformSpeed * Time.deltaTime);
}
}
}
I also have an empty game object named "Game Manager" that manages respawns. It's code looks like this:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public Transform respawnPoint;
public Player player1;
public Player2 player2;
[System.NonSerialized]
public bool respawn;
[System.NonSerialized]
public bool resetSwitch=false;
[System.NonSerialized]
public bool respawnCondition;
void Update ()
{
//if you press r, they can respawn. otherwise, a respawn condition has to be true.
if ((Input.GetKey(KeyCode.R) || respawnCondition==true) && player2.transform.position.x>respawnPoint.transform.position.x
&& respawn==false)
{
respawn=true;
respawnCondition=false;
}
//if respawns true, move them to the respawn points position.
if (respawn==true)
{
resetSwitch=true;
player1.transform.position=new Vector3(respawnPoint.transform.position.x + 1.1f, respawnPoint.transform.position.y, -2f);
player2.transform.position=new Vector3 (respawnPoint.transform.position.x, respawnPoint.transform.position.y, -1f);
respawn=false;
}
}
}
These two pieces of code work together so that every time the player respawns, the switches and their associated platforms are reset. However, I have recently put in a switch that has a bit of an issue–somehow, it works when I start the scene from the beginning, but if I load the scene from somewhere else (the scene is level 3, the player has to get through 1 and 2 before they reach it) the switch mysteriously doesnt reset when i respawn. I honestly have no idea what is going on, so any help would be greatly appreciated.