Hello, I’ve followed the “Unity Multiplayer: Intermediate C# Coding & Networking” course to the “Spawning Building” part. However, when I tried to play and drag the building from canvas to the game scene, the “Unit Spawner” game object was deactivated once I moved the mouse pointer out of its collider or mesh. Do you have suggestions about where to check or how to solve the problem?
Hi there,
This is strange behaviour, but fortunately it shouldn’t be too difficult to figure out. I recommend using a search in your code editor for all references to the unit spawners. See if there is any code that deactivates them. If there is, drop a debug statement in there and figure out why it’s being called.
Also can you show your unit spawner and building buttons in the inspector? See if anything looks weird there.
Do you have any errors in the console? We should try and resolve those as well.
Thank you for your reply.
I can’t find anything wrong from the code, but I could be wrong. Here’s my “UnitSpawner.cs” script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.EventSystems;
public class UnitSpawner : NetworkBehaviour, IPointerClickHandler
{
[SerializeField] private Health health = null;
[SerializeField] private GameObject unitPrefab = null;
[SerializeField] private Transform unitSpawnPoint = null;
#region Server
public override void OnStartServer()
{
health.ServerOnDie += ServerHandleDie;
}
public override void OnStopServer()
{
health.ServerOnDie -= ServerHandleDie;
}
[Server]
private void ServerHandleDie()
{
// NetworkServer.Destroy(gameObject);
}
[Command]
private void CmdSpawnUnit()
{
GameObject unitInstance = Instantiate(unitPrefab, unitSpawnPoint.position, unitSpawnPoint.rotation);
NetworkServer.Spawn(unitInstance, connectionToClient);
}
#endregion
#region Client
public void OnPointerClick(PointerEventData eventData)
{
if(eventData.button != PointerEventData.InputButton.Left) { return; }
if(!isOwned) { return; }
CmdSpawnUnit();
}
#endregion
}
Also, here’s my building button from the Inspector window:
Finally, the error logs in the console are about finding the references at the Start() function, but as it can find later in the Update function, there is no more error. You can check it here:
Hmm, everything looks normal there.
Can you share the unitSpawner in the inspector?
Also, is it possible the unitSpawner is set to inactive in the prefab, so it just spawns inactive?
You can also upload your project here if you like, and I can take a look at it. Just let me know:
https://gdev.tv/projectupload
Sure, I noticed that if I deactivate or remove the Collider component then the building (Unit Spawner) will not be deactivated but I cannot click to spawn the Tank instead. Here’s the Spawner prefab.
I have uploaded the project to the link you requested. Thank you very much for your time and assistance.
Oh I see the issue, your Health Display script is hooked up to the UnitSpawner parent object instead of its health bar. So when you try and view the health and then leave, it deactivates the game object.
Yes, thank you! Your help is very appreciated. I have solved the problem now.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.