Search Unity

GameObject is already being activated or deactivated on moving Platform

Discussion in 'Scripting' started by misanthropowitsch, Jul 20, 2017.

  1. misanthropowitsch

    misanthropowitsch

    Joined:
    Sep 30, 2016
    Posts:
    23
    Hi all,

    My current project features moving platforms.
    The player is a child of the platform as long as he is on it, to keep him in track with the movement.

    The problem with this is, if the player dies while still being part of it, the game crashes

    with: GameObject is already being activated or deactivated

    and refers to: line 5

    Code (CSharp):
    1. void OnCollisionExit2D(Collision2D other) {
    2.  
    3.      if (other.transform.tag == "MovingPlatform")
    4.      {
    5.          transform.parent = null;
    6.              onPlatform = false;
    7.       }
    8. }
    9. }

    What can I do to prevent his from happening? I tried "null" logic.
    (works for the error. Problem with this: Player still thinks, he is part of the platform after respawning and goes for a invisible platform ride on respawn position lol.)

    I tried joints instead of the parenting (I don´t get how this works)

    Is there something I´m missing?

    Edit: Well.. I can ofc skip all the child stuff and add some small box colliders at the platforms edges, to prevent the player from falling of the platform..

    Not the kind of solution I was looking for though.. :/
     
    Last edited: Jul 20, 2017
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Parenting should be fine, and unparenting should also be fine. Do you unparent when death occurs?

    As an unrelated aside, I suggest using transform.SetParent(null); but that should work just the same as the transform.parent = null; call above. I just think it is clearer what is going on.
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Just leave the null check and make sure you unparent on awake/respawn.

    Parenting is definitely the right solution. ... ze proof:



     
  4. misanthropowitsch

    misanthropowitsch

    Joined:
    Sep 30, 2016
    Posts:
    23
    First off, big thanks for your replies!

    Well. I tried to yeah.

    Unfortunately, the unparenting code on respawn is ignored. (More likely, I set it on the wrong position or the code for it is not correct.)

    I don´t want to bother you guys with my scripts that involve my respawnmechanics though.

    I startet with:
    Code (CSharp):
    1.  void OnCollisionExit2D(Collision2D other)
    2.     {
    3.  
    4.         if (thePlayer == null &&  other.transform.tag == "MovingPlatform")
    5.         {
    6.             transform.parent = null;
    7.             onPlatform = false;
    8.         }
    That one fixes the crash. The platformdriving on respawn begins though.

    Code (CSharp):
    1. void Start () {
    2.  
    3.         thePlayer = FindObjectOfType<PlayerControllerWobble>();
    4.  
    5.  
    6.  
    7.         healthCount = maxHealth;
    8.        
    9.  
    10.         objectsToReset = FindObjectsOfType<ResetOnRespawn>();
    11.  
    12.         if(PlayerPrefs.HasKey("CoinCount"))
    13.         {
    14.             coinCount = PlayerPrefs.GetInt("CoinCount");
    15.         }
    16.  
    17.         coinText.text = "Coins: " + coinCount;
    18.  
    19.         if(PlayerPrefs.HasKey("PlayerLives"))
    20.         {
    21.             currentLives = PlayerPrefs.GetInt("PlayerLives");
    22.         } else {
    23.             currentLives = startingLives;
    24.         }
    25.  
    26.  
    27.         livesText.text = "Lives x " + currentLives;
    28.  
    29.         }
    30.  
    31.  
    32.        
    33.            
    34.  
    35.  
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.         if(healthCount <= 0 && !respawning)
    40.         {
    41.             Respawn();
    42.             respawning = true;
    43.         }
    44.            
    45.               if(coinBonusLifeCount >= 100)
    46.             {
    47.                 currentLives += 1;
    48.                 livesText.text = "Lives x " + currentLives;
    49.                 coinBonusLifeCount -= 100;
    50.             }
    51.    
    52.     }
    53.  
    54.     public void Respawn()
    55.  
    56.     {
    57.         currentLives -= 1;
    58.         livesText.text = "Lives x " + currentLives;
    59.  
    60.         if(currentLives > 0)
    61.         {
    62.             StartCoroutine("RespawnCo");
    63.         }else {
    64.             thePlayer.gameObject.SetActive(false);
    65.             gameOverScreen.SetActive(true);
    66.             levelMusic.Stop();
    67.             gameOverMusic.Play();
    68.  
    69.             //levelMusic.volume = levelMusic.volume / 2f;
    70.         }
    71.     }
    72.  
    73.     public IEnumerator RespawnCo()
    74.  
    75.     {
    76.         respawnCoActive = true;
    77.  
    78.         thePlayer.gameObject.SetActive(false);
    79.  
    80.         Instantiate(DeathParticle, thePlayer.transform.position, thePlayer.transform.rotation);
    81.  
    82.         yield return new WaitForSeconds(waitToRespawn);
    83.  
    84.         respawnCoActive = false; //respawnkram
    85.  
    86.         healthCount = maxHealth;
    87.         respawning = false;
    88.         UpdateHeartMeter();
    89.  
    90.         coinCount = 0;
    91.         coinText.text = "Coins: " + coinCount;
    92.         coinBonusLifeCount = 0;
    93.  
    94.  
    95.         thePlayer.transform.position = thePlayer.respawnPosition;
    96.  
    97.         thePlayer.gameObject.SetActive(true);
    98.        
    99.         for(int i =0; i < objectsToReset.Length; i++)
    100.  
    101.         {
    102.            
    103.             objectsToReset[i].gameObject.SetActive(true);
    104.             objectsToReset[i].ResetObject();
    105.         }
    106.     }
    This is where Respawning is handled. Where exactly would you add the unparenting and what kind of code could that be? I tried a lot until now. Nothing works apparently.

    Big sry for the code desert. Especially cause it´s undocumented..
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Second window, line 78, where you turn the player off, if I understand. Isn't that where you die?

    Just put right there, line 78:

    Code (csharp):
    1. thePlayer.transform.SetParent(null);
    Bam. As my Daddy always said, "A million players may die but your transform will be set free."

    Alternately, maybe line 84, so that your corpse doesn't mysteriously linger midair after death.
     
  6. misanthropowitsch

    misanthropowitsch

    Joined:
    Sep 30, 2016
    Posts:
    23
    You´re the real mvp Kurt.. It worked!

    How could I miss that?

    I set it at void Respawn and wondered what´s wrong. Well. Let´s not talk about it.

    :D Your dad is a wise man!

    Thanks again and greetings
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    And your transform is now free. Carry on with your development!
     
    misanthropowitsch likes this.
  8. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13

    Wow!! Thank you so much! I was making a moving platform as well and kept getting this error. Definitely fixed it and don't know how I didn't catch it. All I did is what you said, on the coroutine on respawn, right after setting player to false I put ----->> thePlayer.transform.SetParent(null); <<------ and it working :) :)
     
    Kurt-Dekker likes this.