Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Cannot change GameObject hierarchy while activating or deactivating the parent.

Discussion in 'Scripting' started by RunningRage, Apr 11, 2024.

  1. RunningRage

    RunningRage

    Joined:
    Jun 28, 2023
    Posts:
    5
    Hello all, I am getting an error when I try I switch scenes or end the game with this code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StickyPlatform : MonoBehaviour
    6. {
    7.  
    8.     private void OnCollisionEnter2D(Collision2D collision)
    9.     {
    10.         if (collision.gameObject.name == "Player" && collision.gameObject.transform.parent == null)
    11.         {
    12.             Debug.Log(collision.gameObject.transform.parent != null);
    13.             collision.gameObject.transform.SetParent(transform);
    14.         }
    15.     }
    16.     private void OnCollisionExit2D(Collision2D collision)
    17.     {
    18.         if (collision.gameObject.name == "Player" &&  collision.gameObject.transform.parent != null)
    19.         {
    20.             collision.gameObject.transform.SetParent(null);
    21.         }
    22.     }
    23. }
    As you can see, I tried to check if the parent of the object was or wasn't null before I changed it but this still results in the same error. Someone had posted a thread like this years back but hasn't gotten any answers so I am now posting this to see if anyone can help me
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Ok, so what is the error?
     
  3. RunningRage

    RunningRage

    Joined:
    Jun 28, 2023
    Posts:
    5
    Sorry should have clarified the error
    It is: cannot set the parent of the game object “Player” while activating or deactivating the parent game object “platform-long”. Platform long being the game object this script is attached to
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,486
    I have a hunch that you are calling SetActive() in another collision callback, namely the platform the player is on. If so, you can either combine the reparenting and SetActive code so that the parent is set before SetActive is called. Alternatively you can start a coroutine that yields null and then reparent.


    Btw, Collision2D has a transform property, so there's no need to go through gameObject every time:
    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.name == "Player" && collision.transform.parent == null)
    4.         {
    5.             Debug.Log(collision.transform.parent != null);
    6.             collision.transform.parent = transform;
    7.         }
    8.     }
    9.     private void OnCollisionExit2D(Collision2D collision)
    10.     {
    11.         if (collision.gameObject.name == "Player" &&  collision.transform.parent != null)
    12.         {
    13.             collision.transform.parent = null;
    14.         }
    15.     }
    16.    
    And SetParent() is only really useful if you use the worldPositionStays parameter, otherwise just assign to .parent property.
     
  5. RunningRage

    RunningRage

    Joined:
    Jun 28, 2023
    Posts:
    5
    I didn't remember ever deactivating the platform and I checked my code and I haven't used the setActive function on it, but the error comes when I close the scene or switch scenes. I didn't think that gameobjects deactivate when you switch scenes. Additionally, I also Debug.Logged whether or not the platform was active and it was always active, even when I close the scene but for some reason when I end my game it says I cannot set the parent of "Player" while activating or deactivating "platform-long" (name of platform), which makes no sense because the platform is never deactivated. Should I report as a bug?
     
    Last edited: Apr 15, 2024