Hello all, I have this script which makes the player attached to a platform whenever they come in to contact, and then unattached (in hierarchy) whenever they are not contacting eachother. The problem is, I get the bug: Cannot set the parent of the GameObject ‘Player’ while activating or deactivating the parent GameObject ‘platform-long (1)’. This bug occurs when I end the game or switch scenes while the player and platform are touching. I have done a lot to check whether or not the platform is active and only then set the parent of the player to be the platform but nothing has worked. Finally, I debug.logged whether or not the platform was ever deactivated, and it never was so I do not understand why the error occurs if the gameobject is never deactivated. I have seen and posted question forums but no one has been able to help.
This is the code that is attached to the platform:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StickyPlatform : MonoBehaviour
{
public GameObject platform;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Player")
{
collision.transform.parent = transform;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.name == "Player" && collision.transform.parent != null)
{
if (transform.childCount == 1)
{
collision.transform.parent = null;
}
}
}
}
I did but that was a question I think this might be a bug because I have tried to fix this error in every way possible for a while and also the tutorial I followed did not have this problem
When you unload a scene, everything in said scene is disabled and then destroyed. This is technically what happens when exiting play mode, as the temporary duplicate of your scene gets unloaded and replaced with the instance for editing.
A game object getting destroyed/disabled will still cause collision callbacks, I believe, which is probably what’s causing this error.
Not sure of the solution, honestly. Maybe just manually disabling/destroying the player before enacting the scene load. You can probably just ignore the error when exiting play mode; probably harmless all things considered.
Necro reply, added for those who will find this via a search engine.
spiney199 is spot on I believe. I had the same issue. It was caused by trigger/collision callbacks when a new scene is loaded.
For example. Everything works fine, then once a scene is unloaded (i.e. you restart the level or move to a next level). Everything gets destroyed. The problem is that it doesn’t happen all at once, but some things get destroyed before others. So you might have one object triggering an on exit method and it wants to reparent itself but the parent is destroyed or being deactivated which gives an error.
This unfortunately does not happen every time you unload a scene, sometimes you have to restart the level 10 times or so to get the error which means you could discover it later in your project.
Curiously enough, as far as I can tell, this behavior did not exist up to Unity 2021, it seems to start occurring somewhere around Unity 2022 upwards. Perhaps something was changed in how a scene is unloaded?
Sometimes easy solutions such as activeInHierarchy would work, in others it doesn’t seem to work. You would expect that checking if the parent is active via activeInHierarchy would be a safe bet but perhaps with the way things are destroyed and the C++ objects/garbage collection it doesn’t always work as expected when a scene is unloaded.
I believe that perhaps the old behavior of pre Unity2022, where you didn’t have to worry about what happens when a scene unloads is much preferable and this could indeed be classified as a bug.