0

I'm currently learning how this whole networking thing works in unity. In my code I'm creating a spaceship made from multiple prefabs.

It all starts with a single Hardpoint. A Hardpoint can hold a single object, which will be instantiated later on in the loop.

In the PlayerController (the starting point) i have this code to spawn the first object, the cockpit:

[Command]
void CmdOnConnect() {
    string json = GameObject.Find("TestPlayer").GetComponent<ComponentObject>().ToJSON();
    CompressedComponent compressedComponent = JsonUtility.FromJson<CompressedComponent>(json);
    gameObject.GetComponent<Hardpoint>().Hold(GameObject.Find("Component Repository").GetComponent<ComponentRepository>().cockpit[compressedComponent.componentNumber]);
    gameObject.GetComponent<Hardpoint>().SpawnComponent();
    gameObject.GetComponent<Hardpoint>().RollThroughDecompression(compressedComponent);
    Camera.main.GetComponent<PlayerCamera>().player = gameObject;
}

Next up is the SpawnComponent() code, located in the Hardpoint script:

public void SpawnComponent() {
    Clear();
    CmdSpawn();
}

CmdSpawn, also located in Hardpoint:

[Command]
public void CmdSpawn()
{
    Debug.Log("[COMMAND] Spawning " + holds.name);
    heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
    heldInstance.transform.SetParent(transform);
    NetworkServer.SpawnWithClientAuthority(heldInstance, transform.root.gameObject);
}

And finally RollThroughDecompression, which just calls the Decompress() function:

public void RollThroughDecompression(CompressedComponent c) {
    heldInstance.GetComponent<ComponentObject>().Decompress(c);
}

And just to leave no information out, Decompress():

public void Decompress(CompressedComponent c) {
    componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
    componentNumber = c.componentNumber;
    UpdateHardPoints();
    GameObject[] typeRepository = GetRepository(componentType);

    //update children 
    int point = 0;
    foreach (Transform child in transform)
    {
        Hardpoint hardpoint = child.GetComponent<Hardpoint>();
        if (hardpoint != null) {
            if (c.hardpoints[point] != null) {
                //get the hardpoint's repository
                GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
                //set the hardpoint to hold this object
                hardpoint.Hold(hardpointRepo[c.hardpoints[point].componentNumber]);
                hardpoint.SpawnComponent();
                hardpoint.RollThroughDecompression(c.hardpoints[point]);
                point++;
            }
        }
    }
}

Sorry the code's a little messy/confusing but I've been driven up the walls trying to figure out why newly spawned objects don't have client authority with the exception of the first object spawned (likely because it's called from the PlayerController). I've been stuck on this problem for days now. Newly spawned objects are being set as children of the local player object and are even spawned with NetworkServer.SpawnWithClientAuthority yet when testing:

Trying to send command for object without authority. when calling CmdSpawn().

NetworkManager: enter image description here

The result i'm getting:

enter image description here

As you can see, the cockpit (very first part) gets spawned as expected. but parts mounted on those Hardpoints don't. To clarify, the EmptyHardpoint is just that. A hardpoint with no children, just an empty game object with the hardpoint script and playercontroller attached to it. The cockpit prefab also includes the img and hardpoints

Kevin Kuyl
  • 1,215
  • 1
  • 17
  • 31

1 Answers1

0

I guess you have forget to add your child Spawn in the Spawn list of NetworkManager.

enter image description here

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • Incorrect. That was my first guess as well, but the first component that should be spawned, does get spawned, others dont. i'll post a screenshot in a moment. – Kevin Kuyl Dec 05 '16 at 06:27
  • if you are spawning separately so assign separately – Muhammad Faizan Khan Dec 05 '16 at 06:43
  • Sorry, looked at it wrong. Updated, but unfortunately the problem persists. – Kevin Kuyl Dec 05 '16 at 06:47
  • I'm getting more warnings now though. all `hardpoints` have a networkidentity because of the script attached, it's a `networkbehavior` so i can't remove it. I'll shift some code around, see if i can get rid of the warning messages and get back to you on this. i think we're on to something here, thanks so far! – Kevin Kuyl Dec 05 '16 at 06:59