Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Mirror Networking - Tower Defense/Strategy Game

Discussion in 'Multiplayer' started by w0tness, Nov 15, 2023.

  1. w0tness

    w0tness

    Joined:
    Mar 21, 2023
    Posts:
    1
    Hey guys

    So, I'm currently a second year student and I'm struggling with mirror networking. Specifically, I'm trying to add functionality to my Tower Defense game so that each player can spawn creeps in their respective base, which in turn moves to the other player's base to do damage to their 'Main Building'.

    Currently, I have two Wave-Spawners placed in the scene itself (Player1WaveSpawner and Player2WaveSpawner).

    Now, I'm busy with a script that allows the player to click on a button which will then spawn the creep. Unfortunately, when I try to assign the function in the 'On Click' section of the button in the inspector after putting in the script, I can't find the function (On Click ()). I was wondering if anyone had any ideas what I could do here?

    Here is the full script I'm busy with. Any help in this regard will be immensely appreciated.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Mirror;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;

    public class WaveSpawnerPlayer1 : NetworkBehaviour {

    [SerializeField] GameObject canvas;

    public uint myPlayerID;

    [SerializeField] private GameObject unitPrefab;

    [SerializeField] private Transform unitSpawnPoint;

    void OnEnable()
    {
    if (isLocalPlayer) {
    canvas.SetActive(false);
    Debug.Log($"ID = {GetComponentInParent<RTSCam>().GetNetworkID()}");
    myPlayerID = GetComponentInParent<RTSCam>().GetNetworkID();
    }
    }

    public void OnClick(uint _id)
    {
    if (_id == myPlayerID)
    {
    Debug.Log("clicking at the moment");
    EnableCanvas();
    CmdSpawnUnit();
    }
    }


    void EnableCanvas()
    {
    canvas.SetActive(true);
    }

    [Command(requiresAuthority = false)]
    private void CmdSpawnUnit() {

    GameObject unitSpawn = Instantiate(unitPrefab, unitSpawnPoint.position, unitSpawnPoint.rotation);
    Debug.Log("spawning creep");
    NetworkServer.Spawn(unitSpawn, connectionToClient);
    }
    }
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    It would depend upon the kind of button you are using.
    However, if you are using the legacy button then I believe the listener is just expecting a method with no parameters.
    As in if you were to have a reference to the button in your script and assign the listener in say the Start method it would look something like:
    Code (CSharp):
    1. m_Button.onClick.AddListener(OnClick);
    However, it looks like you are using the Mirror netcode solution so anything beyond the above suggestion you will need to get assistance from their GitHub issues page or discord support channel.