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");
}
Related
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
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
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").
I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision.
This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen;
}
}
}
and this is the code for my GameOverScreen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = "Score:" + score.ToString();
}
}
You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.Setup();
}
}
}
The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.score = theScore;
GameOverScreen.gameObject.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public int score;
private void OnEnable()
{
pointsText.text = "Score:" + score.ToString();
}
}
The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.
I had been searching so much to solve my problem but I found nothing.
So, what is my problem?
I have a class that extends from UnityEvent and it has 2 virtual methods and it has public identifier in another file that should call those methods. But when I put my script in inspector nothing happens (except root virtual methods).
The class:
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
namespace Namespace {
[System.Serializable]
public class SomeClass : UnityEvent {
public virtual void OnSomething(GameObject objectSender, GameObject objectAction) {
}
public virtual void OnSomethingElse(GameObject objectSender, GameObject objectAction) {
}
}
}
I overrided those methods in a file and but in variable "script" shown below but they are not overrided.
And using of SomeClass:
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
namespace Namespace {
public class ActionListener : MonoBehaviour {
public SomeClass script;
// When I lose last hope
// private void Start() {
// script.Invoke();
//}
public void OnHover(GameObject sender, GameObject action) {
script.OnHoverListener(sender, action);
}
public void OnClick(GameObject sender, GameObject action) {
script.OnClickListener(sender, action);
}
}
}
I have solved this problem using MonoBehaviour instead UnityEvent.
And not it looks like this
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
namespace Namespace {
public class SomeClass : MonoBehaviour {
public virtual void OnSomething(GameObject objectSender, GameObject objectAction) {
}
public virtual void OnSomethingElse(GameObject objectSender, GameObject objectAction) {
}
}
}
And in use
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
namespace Namespace {
public class ActionListener : MonoBehaviour {
public SomeClass script;
public void OnHover(GameObject sender, GameObject action) {
script.OnSomething(sender, action);
}
public void OnClick(GameObject sender, GameObject action) {
script.OnSomethingElse(sender, action);
}
}
}