Server/Client Authority issue - Mirror in Unity - c#

I am trying to make a string of text send from the host to the client and vice versa. This is done by sending a string of text from the players' scripts to a gameobject with the the following script on it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Mirror;
public class AnswerReceiver : NetworkBehaviour
{
// Start is called before the first frame update
void Start()
{
DontDestroyOnLoad(this);
}
[SyncVar] public string getfromcards;
[SyncVar] public string SendToOthers;
public NetworkIdentity identity;
public void CardClicked()
{
if(isServer) {
ServerCardClicked();
}
else
{
ClientCardClicked();
}
}
[Command]
public void ClientCardClicked()
{
SendToOthers = getfromcards;
}
[ClientRpc]
void ServerCardClicked()
{
SendToOthers = getfromcards;
}
}
When the script does this, I am able to get text from the client to the server but sending text to the script from the host produces the following error: "Trying to send command for object without authority. AnswerReceiver.ClientCardClicked"
Anyone know whats up?
Thanks

Related

LootLocker submitscore script doesnt work

When the game starts, the session start script works fine, but the sending of points does not respond, the debuglog does not respond either
using LootLocker.Requests;
using UnityEngine;
public class LeaderBoard: MonoBehaviour
{
public void SubmitScore()
{
int leaderboardID = 11584;
int score = PlayerPrefs.GetInt("high");
string playerID = PlayerPrefs.GetString("PlayerID");
LootLockerSDKManager.SubmitScore(playerID, score, leaderboardID, (response) =>
{
if (response.success)
{
Debug.Log("Successfully uploaded score");
}
else
{
Debug.Log("Failed" + response.Error);
}
});
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
LeaderBoard leaderBoard;
void Update()
{
if (game.isComplete)
{
leaderBoard.SubmitScore();
}
}
objects with scripts in the game added
I tried following the official tutorial https://www.youtube.com/watch?v=u8llsk7FoYg&t=605s and i know its something supersimple but i dont know what pls help me

Having troubles with unity camera depth texture

I am trying to implement a shader that makes things darker the further from the camera they are,the way it works underwater, but when I try to work with the camera depth ,I get "error CS0103: The name 'depthTextureMode' does not exist in the current context".
This is the code I tried to run
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode,ImageEffectAllowedInSceneView]
public class FogEffect : MonoBehaviour
{ public Material _mat;
void Start()
{
GetComponent<Camera>().depthTextureMode = depthTextureMode.Depth;
}
void Update()
{
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source,destination,_mat);
}
}
DepthTextureMode is an enum and the first letter needs to be uppercase.
This compiled and run without issues on my machine.
using UnityEngine;
public class FogEffect : MonoBehaviour
{
void Start()
{
GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
}
}

Photon only showing one player

in photon the first person to join can see both players but second to join cant see the first. anyone know why?
connect to server script
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;
public class Connecttoserver : MonoBehaviourPunCallbacks
{
private void Start()
{
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
PhotonNetwork.JoinRoom("Server");
}
public override void OnJoinedRoom()
{
SceneManager.LoadScene("Game");
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
Debug.Log(message);
PhotonNetwork.CreateRoom("Server");
}
}
spawn players script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class spawnplayers : MonoBehaviour
{
public GameObject playerprefab;
public float minx, maxx, minz, maxz;
private void Start()
{
Vector3 randompos = new Vector3(Random.Range(minx,maxx),1,Random.Range(minz,maxz));
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate(playerprefab.name, randompos, Quaternion.identity);
myPlayerGO.GetComponent<playermovement>().enabled = true;
myPlayerGO.GetComponentInChildren<CameraLook>().enabled = true;
myPlayerGO.GetComponentInChildren<Camera>().enabled = true;
}
}
Screen 1
Screen 2
Anybody know a solution?? I have been trying to solve this for days but to no success
what ive tried
adding heaps of debug logs
adding photonview and transform
crying myself to sleep
Make sure both clients are connected to the same server (Region if Photon Cloud) and to the same Virtual Application (if Photon Cloud). You could make use of SupportLogger. Check Matchmaking Checklist.
Use PhotonNetwork.LoadLevel instead of SceneManager.LaodLevel.
Also, you could make use of JoinOrCreateRoom method as a shortcut instead of trying to join then creating one.
I would start by finishing PUN Basics Tutorial first or following one of YouTube tutorials about PUN.
When players join map, use PhotonNetwork.LoadLevel("SceneName") instead of SceneManager.LoadScene("SceneName").

How to storage in runtime the data of GameObject? - Unity

I want to save the attribute of a gameObject but in runtime without Serializing anything. This I want to do when the object is selected. That way when I touch the paint button for example, paint the object that was selected.
What I tested so far is the use of the event OnMouseDown that is in the GameObject script and I also made another empty GameObject with the script that saves the attribute, but I can't save for example the name of the selected object. Although it shows it in the Debug.Log but returns log at the time of assigning it.
I applied the same logic in a new project with ObjSelect, SelectScript, ButtonsEventsScript scripts with the same prefabs and GameObjects but without Vuforia and it worked great.
Repository: https://github.com/emicalvacho/MapaMentalAR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjSelect : MonoBehaviour
{
private string nombre;
void OnEnable() {
SelectScript.OnName += HandlerOnName;
}
void OnDisable() {
SelectScript.OnName -= HandlerOnName;
}
void HandlerOnName(string str){
Debug.Log("NOMBRE DEL OBJETO SETEADO: " + str);
nombre = str;
}
public string getNombre(){
return nombre;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class SelectScript : MonoBehaviour
{
public GameObject cube;
private GameObject findObj;
public GameObject gameObjectManager;
public delegate void _OnName (string str);
public static event _OnName OnName;
private bool band;
public void OnMouseDown()
{
if(OnName != null)
OnName(cube.name);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonsEventsScript : MonoBehaviour
{
public ObjSelect script;
private GameObject obj;
public void ChangeColor(){
Debug.Log("OBJETO A PINTAR ES: " + script.getNombre());
}
}
I don't have any error messages, but it returns a null instead of the name of the selected GameObject.
If anyone has any other way of doing this, I ask you to help me for more than a week that I've been stuck. I already asked in other forums and nobody gives me a solution or a way to solve.

How can I trigger OnPlayerConnected while using NetworkManager Unity?

I am using UNet and NetworkManager component. I am trying to when the player connected to server just say to me 'I am connected'. I must serialize that. I am using NetworkBehaviour, I think this may lead to failure. But how can I serialize that ?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class MultiPlayerOyunKontrol : NetworkBehaviour
{
void OnPlayerConnected(NetworkPlayer player)
{
Debug.Log("Player baglandi"+this.transform.name);
}
}
The OnPlayerConnected function is not even part of UNet API. It's part of Unity's legacy network API. This is how should have been be used:
public class MultiPlayerOyunKontrol : MonoBehaviour
{
void OnPlayerConnected(NetworkPlayer player)
{
Debug.Log("Player baglandi"+this.transform.name);
}
}
not
public class MultiPlayerOyunKontrol : NetworkBehaviour
{
void OnPlayerConnected(NetworkPlayer player)
{
Debug.Log("Player baglandi"+this.transform.name);
}
}
Basically, OnPlayerConnected has nothing to do with NetworkBehaviour so it wouldn't work unless you are using the old Unity network API which you're not.
Below is a proper way to see when client is connected or disconnected with UNet:
void Start()
{
NetworkServer.Listen(9000);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetworkServer.RegisterHandler(MsgType.Error, OnError);
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Client Connected");
}
public void OnDisconnected(NetworkMessage netMsg)
{
Debug.Log("Disconnected");
}
public void OnError(NetworkMessage netMsg)
{
Debug.Log("Error while connecting");
}

Categories

Resources