Why won't my game object reappear?

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()
    {

    }
}

What does the debug.log output look like? What is the count of the log messages?

Well when I start the game the idle will go into the hundreds constantly going over and and over. Then when I jump on the platform it switches to crumbled once and then states idle one more time but idle doesn’t constantly repeat in the log over and over anymore.

I know this is probably a stupid question but the game object that you are disabling isnt the game object that has this script, is it?

That would explain the log stopping.

The game object that I am disabling is the one that has this script

Then when you disable it, the script itself will stop running right? Remember the script is part of the game object.

1 Like

If you wanted to have this, consider having the script on a parent object and the platform as a child. That said - I am not sure if collider event propergate up to the parent object. You might have to test that.

If it doesnt work, maybe write make the child object notify the parent when the player enters/leaves it.

1 Like

Hmmm that makes alot of sense… I just attached it to the player made a few slight tweaks to the script and its working now :smile: Thanks Korno

1 Like