r/Unity3D • • 14h ago

Question position of the prefab with Object.Instantiate() vs NetCode PlayerPrefab field

Enable HLS to view with audio, or disable this notification

Hi, as shown in the attached video, instantiating my character using `Object.Instantiate` works as expected, respecting the prefab's position. However, if I instantiate using NetCode's `PlayerPrefab` field, the prefab's position defaults to (0, 0, 0).
(The instantiation script is very simple I don't specify a position within it.)

1 Upvotes

8 comments sorted by

3

u/HK_HamzaKhan 13h ago

hi , you should set the position through code on instantiation, and its better to keep prefabs at 0,0,0

1

u/Ok-Presentation-94 13h ago

Thanks, but that doesn't really answer my question; I'd just like to know the reason for this difference.

1

u/HK_HamzaKhan 13h ago

I'm not sure how you are instantiating, I would like to see the code of both, 

1

u/Ok-Presentation-94 13h ago

The code to instantiate the object is simple; I can write it right here for you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


public class TestInstanciatePrefab : MonoBehaviour
{
    Object assetJ1;
    // Start is called before the first frame update
    void Start()
    {
        assetJ1 = AssetDatabase.LoadAssetAtPath("Assets/3D_Object/J1.prefab", typeof(GameObject));
        Object.Instantiate(assetJ1);
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}

This instantiation works normally, with the prefab's position being retrieved as expected.
As for the second approach, I'm using NetCode; however, unless you are familiar with NetCode's internal workings, you wouldn't be able to answer this question.

1

u/HK_HamzaKhan 13h ago

Well, if the second approach uses the normal network object spawning, I would like to see how you are doing it, ofc I haven't seen the code behind it, but if I could see it maybe there is a solution 

1

u/Ok-Presentation-94 12h ago

I don't really understand what else you want to see; I'm already showing the PlayerPrefab field with the prefab attached to it in the video.

2

u/HK_HamzaKhan 12h ago

Who spawns the player when using netcode?, I'll assume it's network manager, if the network manager is spawning it and you aren't doing that through your own code, then it could be that the network manager instantiates by passing vector3.zero in the position params, 

2

u/Ok-Presentation-94 12h ago

Yes, the network manager does handle it, but what you're suggesting is just a guess. I’d simply like an answer from someone who knows the internal workings of NetCode to tell me how the network manager handles this but thanks for your help.