Okay so I am trying to get a platform that once the player stands on to long it disappears. I got it to disappear, but I cannot get it to reappear after the player falls back to the ground. I added a debug log to make sure it is going back to the idle state from the crumbled state which it is, so that is not the problem. I am not sure what to do. The object I am talking about is the variable crumbleToDeactivate in the StateIdle function.
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class PlatformStateMachine : MonoBehaviour {
private enum PlatformStates
{
IDLE,
CRUMBLED,
MOVING,
NUM_STATES
}
[SerializeField]
private float maxPlayerStayCrumbleTime = 0.60f;
[SerializeField]
private float timePlayerStayCrumbleTime = 0.0f;
[SerializeField]
private GameObject crumbleToDeactivate;
Dictionary<PlatformStates, Action> fsm = new Dictionary<PlatformStates, Action>();
PlatformStates curState;
private bool onCrumble = false;
private bool platformCrumbled = false;
// Use this for initialization
void Start ()
{
fsm.Add (PlatformStates.IDLE, StateIdle);
fsm.Add (PlatformStates.CRUMBLED, StateCrumbled);
fsm.Add (PlatformStates.MOVING, StateMoving);
SetState (PlatformStates.IDLE);
}
// Update is called once per frame
void Update ()
{
fsm [curState].Invoke ();
Debug.Log (curState);
}
void OnTriggerStay2D(Collider2D col)
{
if (col.tag == "Player")
{
//if(col.tag == "Crumble")
//{
timePlayerStayCrumbleTime += Time.deltaTime;
onCrumble = true;
//}
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "Player")
{
//if(col.tag == "Crumble")
//{
timePlayerStayCrumbleTime = 0.0f;
//}
}
}
void SetState(PlatformStates nextState)
{
curState = nextState;
}
void StateIdle()
{
crumbleToDeactivate.SetActive (true);
if (timePlayerStayCrumbleTime >= maxPlayerStayCrumbleTime)
{
SetState(PlatformStates.CRUMBLED);
}
}
void StateCrumbled()
{
if (onCrumble == true)
{
crumbleToDeactivate.SetActive(false);
platformCrumbled = true;
}
if (platformCrumbled == true)
{
timePlayerStayCrumbleTime = 0.0f;
SetState(PlatformStates.IDLE);
}
}
void StateMoving()
{
}
}