Search Unity

Resolved Struggle with Setting parent to an object Netcode using NetworkObject.TrySetParent

Discussion in 'Netcode for GameObjects' started by Sulaiman12, Jan 10, 2023.

  1. Sulaiman12

    Sulaiman12

    Joined:
    May 28, 2020
    Posts:
    5
    I'm trying to SetParent of all vehicles in the scene to an gameobject. all the objects have NetworkObject
    I'm spawning the Vehicles from the server first a CarContainer and then a vehicle. after that in update loot I'm checking if object are spawned then I am trying to vehicles.TrySetParent(carContainer) but it's not working now here is my code.

    Code (CSharp):
    1. public void SpawnObjectOnServerStart()
    2.         {
    3.             carContainer = Instantiate(carContainer);
    4.             carContainer.Spawn(true);
    5.            
    6.             // trafficSystem.LoadCars(false, 0);
    7.             var fgWayPoint = FindObjectsOfType<FCGWaypointsContainer>();
    8.             Debug.Log(fgWayPoint.Length+" FG WayPoint");
    9.             for (var i = 0; i < MaxNpcVehicles; i++)
    10.             {
    11.                 var veh = Instantiate(npcVehiclePrefab[Random.Range(0, npcVehiclePrefab.Length)], fgWayPoint[i].AvanceNode(0, 0, 6),
    12.                     fgWayPoint[i].NodeRotation(0, 0));
    13.                 veh.GetComponent<TrafficCar>().sideAtual = 0;
    14.                 veh.GetComponent<TrafficCar>().atualWay = fgWayPoint[i].transform;
    15.                 veh.GetComponent<TrafficCar>().atualWayScript = fgWayPoint[i];
    16.                 var nObject = veh.GetComponent<NetworkObject>();
    17.                 nObject.Spawn(true);
    18.                 _parentVehicles.Add(nObject);
    19.                 // var isParented = nObject.TrySetParent(carContainer.GetComponent<NetworkObject>());
    20.                 // Debug.Log("Vehicle has parent: "+isParented);
    21.             }

    Code (CSharp):
    1. private void Update()
    2.         {
    3.             if (!IsServer) return;
    4.             if (carContainer.IsSpawned)
    5.             {
    6.                 if (_parentVehicles.Count < 1) return;
    7.                 for (var i = 0; i < _parentVehicles.Count; i++)
    8.                 {
    9.                     if (!_parentVehicles[i].IsSpawned) continue;
    10.                     if (_parentVehicles[i].transform.parent != null)
    11.                     {
    12.                         _parentVehicles.Remove(_parentVehicles[i]);
    13.                         i--;
    14.                         continue;
    15.                     }
    16.  
    17.                     // _parentVehicles[i].transform.SetParent(carContainer.transform);
    18.                     Debug.Log(_parentVehicles[i].TrySetParent(carContainer) +
    19.                               $" {_parentVehicles[i].name} trying to parent");
    20.  
    21.                 }
    22.             }
    23.         }
    Please Guide me on what I'm missing. as it's giving me false I suppose it's reverting the network object to its original state. but what I'm missing I've followed the documentations and trying to solve this from a week by myself.
     
  2. Sulaiman12

    Sulaiman12

    Joined:
    May 28, 2020
    Posts:
    5
    Fixed I disabled the networktransform before parenting and spawning. after object is spawned it is successfully parented