Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why won't my game object reappear?

Discussion in 'Scripting' started by wiredbyalex, Jun 13, 2015.

  1. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. using System;
    5. using System.Collections.Generic;
    6.  
    7. public class PlatformStateMachine : MonoBehaviour {
    8.  
    9.     private enum PlatformStates
    10.     {
    11.         IDLE,
    12.         CRUMBLED,
    13.         MOVING,
    14.        
    15.         NUM_STATES
    16.     }
    17.  
    18.     [SerializeField]
    19.     private float maxPlayerStayCrumbleTime = 0.60f;
    20.     [SerializeField]
    21.     private float timePlayerStayCrumbleTime = 0.0f;
    22.     [SerializeField]
    23.     private GameObject crumbleToDeactivate;
    24.  
    25.     Dictionary<PlatformStates, Action> fsm = new Dictionary<PlatformStates, Action>();
    26.     PlatformStates curState;
    27.     private bool onCrumble = false;
    28.     private bool platformCrumbled = false;
    29.  
    30.  
    31.     // Use this for initialization
    32.     void Start ()
    33.     {
    34.         fsm.Add (PlatformStates.IDLE, StateIdle);
    35.         fsm.Add (PlatformStates.CRUMBLED, StateCrumbled);
    36.         fsm.Add (PlatformStates.MOVING, StateMoving);
    37.  
    38.         SetState (PlatformStates.IDLE);
    39.     }
    40.    
    41.     // Update is called once per frame
    42.     void Update ()
    43.     {
    44.         fsm [curState].Invoke ();
    45.         Debug.Log (curState);
    46.     }
    47.  
    48.     void OnTriggerStay2D(Collider2D col)
    49.     {
    50.         if (col.tag == "Player")
    51.         {
    52.             //if(col.tag == "Crumble")
    53.             //{
    54.                 timePlayerStayCrumbleTime += Time.deltaTime;
    55.                 onCrumble = true;
    56.             //}
    57.         }
    58.     }
    59.    
    60.     void OnTriggerExit2D(Collider2D col)
    61.     {
    62.         if (col.tag == "Player")
    63.         {
    64.             //if(col.tag == "Crumble")
    65.             //{
    66.                 timePlayerStayCrumbleTime = 0.0f;
    67.             //}
    68.         }
    69.     }
    70.  
    71.     void SetState(PlatformStates nextState)
    72.     {
    73.         curState = nextState;
    74.     }
    75.  
    76.     void StateIdle()
    77.     {
    78.         crumbleToDeactivate.SetActive (true);
    79.         if (timePlayerStayCrumbleTime >= maxPlayerStayCrumbleTime)
    80.         {
    81.             SetState(PlatformStates.CRUMBLED);
    82.         }
    83.     }
    84.  
    85.     void StateCrumbled()
    86.     {
    87.         if (onCrumble == true)
    88.         {
    89.             crumbleToDeactivate.SetActive(false);
    90.             platformCrumbled = true;
    91.         }
    92.         if (platformCrumbled == true)
    93.         {
    94.             timePlayerStayCrumbleTime = 0.0f;
    95.             SetState(PlatformStates.IDLE);
    96.         }
    97.     }
    98.  
    99.     void StateMoving()
    100.     {
    101.  
    102.     }
    103. }
    104.  
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    What does the debug.log output look like? What is the count of the log messages?
     
  3. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    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.
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    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.
     
  5. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    The game object that I am disabling is the one that has this script
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Then when you disable it, the script itself will stop running right? Remember the script is part of the game object.
     
    wiredbyalex likes this.
  7. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    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.
     
    wiredbyalex likes this.
  8. wiredbyalex

    wiredbyalex

    Joined:
    Jun 6, 2015
    Posts:
    10
    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 :D Thanks Korno
     
    Korno likes this.