Get server to call function?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class MapGen : NetworkBehaviour {

[Server] void DisplayMap() //creates prefabs
    {
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                count += 1;
                if(map [x,y] == 0){
                    float holdX;
                    float holdY;
                    if (y % 2 == 0)
                    {
                        holdX = x + 0.5f;
                    }
                    else
                    {
                        holdX = x;
                    }
                    holdY = y - ((float)y) / 8;
                
                    GameObject cloneGameObject = (GameObject)Instantiate(hex, new Vector3(holdX, 0, holdY), Quaternion.Euler(270f, 90f, 0f));
                
                    cloneGameObject.AddComponent<Tile>();
                    cloneGameObject.GetComponent<Tile>().visible = false;
                    cloneGameObject.GetComponent<Tile>().SetHole(); //NOT CALLING ON SERVER
                
                    cloneGameObject.GetComponent<Tile>().SetIdentifier(count);
                
                    tileDict.Add(count, cloneGameObject.GetComponent<Tile>());
                    NetworkServer.Spawn(cloneGameObject);
                }
        }
    }
}
}
using System.Collections;
using System;
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;
//Basic skeleton of Tile class which will be used as grid node
public class Tile : NetworkBehaviour
{ 

    [SyncVar]public bool visible = true;
        Renderer rend;

    [Server]public void SetHole(){
        Debug.Log ("SETHOLE CALLED");
        if (!isServer) {
            return;
        }
        Debug.Log ("THE SERVER IS CALLING SETHOLE");
        rend = GetComponent<Renderer> ();
        if (!visible) {
            rend.enabled = visible;
        }
        isHole = true;
        blocked = true;
    }
}

I’m not able to get the server to call SetHole(), what am I doing wrong?

The prefab that you’re instantiating…is “Server Only” or “Local Player Authority” checked?

I had neither checked, when I try checking Server Only the prefab isn’t even spawned on clients, if local authority is checked then it will spawn but effectively nothing is changed. Either way I’m never getting the message THE SERVER IS CALLING SETHOLE

Are you at least getting the first log message “SETHOLE CALLED”?

Yep, I’m getting SETHOLE CALLED – could it maybe be that the server can’t send a debug message? Because I’m noticing that their renderers arenot visible (maybe in lower frequency than normal though?)

Nope. Since you’re getting “SETHOLE CALLED” that means the method IS firing. Nothing after if (!isServer) { is being fired because if it’s not the server, you’re returning from the method. This means that the GameObject that the Tile script is attached to, does not belong to the server. Is it an object already in the scene when the scene starts?

The game object is already in the scene, I didn’t notice the tile and the tilePrefab would have different network identity settings from the game object, should both the prefab and game object be set to server only?

Setting the prefab to be server only means it won’t spawn on clients so that musn’t be right. I tried all the combinations of network identity settings, nothing seems to get the message THE SERVER IS CALLING SETHOLE

No. You can’t have the object already in the scene (from my understanding). Any object that uses NetworkBehaviour needs to be spawned after the server has become active. Scene objects don’t belong to the server or the client.

You’re going to have to add some code somewhere that spawns your Tiles after the server has become active.

Okay! I’ll try that, I’m quite hopeful.

I removed the Tile from the scene, and as is they are being spawned as the server becomes active but I still have all the same old problems. My GameManager and MapGenerator also have network identities, but I don’t know if they’re technically “objects”, do they have to be created after the server is active also?

Yes. AFAIK everything that has a NetworkIdentity must be assigned to either the Server or a connected Client, which means it has to be created after the server is actually running.

My GameManager, MapGenerator and Tiles are all being created when the server starts running and I’m still having the same issue :frowning:

Okay so if I move the call to SetHole() to after it’s spawned, I’m getting the message THE SERVER IS CALLING SETHOLE. It’s still not working but this is definitely progress.

I effectively reversed what my initial problem was so I’ve made a new thread: Call function on clients from server? - Unity Engine - Unity Discussions