Unity Freeze after Instantiate

So I trained the AI with accordingly to the “Making a New Learning Environment” guide and now when I created a new prefab with the same parameters like training model and Instantiate it on the new scene using the classical unity way. My Unity freezes for 1-2 s, cannot find the reason becasue the profiler only notices 215 ms freeze not the longer one.

In attachment I give you the sample code where the freeze appear and the prefab what is instantiating. In my
NetworkRollerAgent script I only taking the rigidbody components and the overriden methods like are CollectObservations, OnActionReceived and OnEpisodeBegin are not taking any action when the target (player is null) so it should be sth intrinsic of BehaviourParameters in ml-agents. Im using the Unity 2023.1.2f1 and ML Agents 2.0.1

EDIT!: I noticed that the problem is in a OnEnabled method of Agent.cs class but dont have an access to modify or debug it.

Im alos upload this behaviour on youtube so check out:

.


Ok, so I removed the big freeze by adding in some managers in the Awake method the:

Academy.Instance.InferenceSeed = 1;
Academy.Instance.EnvironmentStep();

and in ActionSegment.cs i add the return if the Lenght is equals 0 in the setter of this "
public T this[int index]"

to register communacator in Academy because beofre that the Academy.IsInitialized was false, and after true.

Now only the “small” lag exist which is trackable by proffiler and it is a GenericWorker Execute method: