Photon only showing one player - c#

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").

Related

Assign a tag to an object in a unity

I'm making a subway simulator, I want the StationBox to always have the Created tag after pressing a button, but it goes back to the previous value when the game is restarted, how can I solve this?
Update: I will have more than 1 station, I would like to make a universal script for all
using System;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class AddStation : MonoBehaviour
{
public InputField Input;
public Text StationName;
public GameObject Button;
public GameObject CreateStation;
public GameObject StationBox;
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created";
}
}
}
If I understand correctly, you are trying to find a way to save game data so as not to lose it when you restart the game.
To do this the most practical solution is PlayerPrefs.
You can find a lot of documentation online because it's a simple concept.
However for your problem you can do this:
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created”;
PlayerPrefs.SetInt(“Created”, 1);
}
}
void Start()
{
if (PlayerPrefs.HasKey(“Created”))
if (PlayerPrefs.GetInt(“Created”) == 1)
StationBox.tag = "Created”;
}

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;
}
}

C# Unity Queue NullReferenceException

I am trying to create a simple dialogue system for my game in Unity. I've set up a special trigger for a Dialogue to start and the code is passing right variable but somehow it gets stuck at clearing the queue and throws a NullReferenceException.
I've seen through debugger that all variables and triggers work perfectly fine till cs:27 inside DialogueManager.cs. I've also checked the inspector to make sure everything is correctly assigned.
Dialogue class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3,10)]
public string[] sentences;
}
DialogueTrigger class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPC_DialogTrigger : MonoBehaviour
{
// Player
public Transform player;
// GameMaager to close
public GameObject Gameplay;
// Camera and Canvas to turn on
public GameObject DialogueManager;
// NPC Details
public GameObject InteractionNPCNameTextField;
public Transform interactionTransform;
public float radius = 3f;
private bool isBusy = false;
// DialogueStart
public GameObject DialogueStart;
void Start()
{
InteractionNPCNameTextField.gameObject.SetActive(false);
}
void Update()
{
float distance = Vector3.Distance(player.position, interactionTransform.position);
if (distance <= radius)
{
if (isBusy == false)
{
InteractionNPCNameTextField.gameObject.SetActive(true);
if (Input.GetKeyDown(KeyCode.E))
{
Dialogue();
Debug.Log("Started Dialogue procedure.");
}
}
}
else if (distance > radius)
{
InteractionNPCNameTextField.gameObject.SetActive(false);
}
}
public void Dialogue()
{
Gameplay.SetActive(false);
DialogueManager.SetActive(true);
DialogueStart.GetComponent<DialogueStart>().TriggerDialogue();
Debug.Log("Triggered Dialogue.");
}
void OnDrawGizmosSelected()
{
if (interactionTransform == null)
{
interactionTransform = transform;
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(interactionTransform.position, radius);
}
}
DialogueStart class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueStart : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
Debug.Log("Dialogue sent to dialogue manager.");
}
}
DialogueManager class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public Text nameText;
public Text DialogueText;
private Queue<string> sentences;
public GameObject DialogueManagerUI;
void Start()
{
if (sentences == null)
{
sentences = new Queue<string>();
}
}
public void StartDialogue (Dialogue dialogue)
{
Debug.Log("Received dialogues: " + dialogue);
nameText.text = dialogue.name;
Debug.Log("Start Dialogue: " + sentences.Count);
sentences.Clear();
Debug.Log("Sentences Cleared: " + sentences);
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
DialogueText.text = sentence;
}
void EndDialogue()
{
Debug.Log("End of conversation.");
}
private void Update()
{
if (DialogueManagerUI.activeInHierarchy)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
else if (!DialogueManagerUI.activeInHierarchy)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}
Visual Studio doesn't give me any errors.
Unity error code -> Screenshot
Debugger right before issue -> Screenshot2
Seems to me like the Queue is never assigned to sentences and it remains empty.
If it is so - why?
It sounds like DialogueManager.StartDialogue probably via DialogueStart.TriggerDialogue is called from somewhere in either Awake or at least before your Start was executed.
Especially
DialogueManager.SetActive(true);
lets assume the DialogueManager object is inactive at first anyway. So maybe your stuff is called before it is set to active.
This might also be due to the Start where you set
InteractionNPCNameTextField.gameObject.SetActive(false);
so any component on this GameObject might not have its Start method getting called. Maybe you referenced the wrong GameObject here?
Debugging would help to figure out in which order your methods get called.
In general my thumb rule for initializing is
Do everything that depends only on yourself in Awake
Do everything that depends on other components being setup already in Start
This way you can (almost) always be sure that stuff is already initialized when you need it.
However, these are only guesses but
The simple solution here:
You could already solve this by simply initializing the sentences right away using
private readonly Queue<string> sentences = new Queue<string>();
now it is definitely initialized even before Start or Awake get called!

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.

Unity SetActive() doesn't activate object

I have a problem that I can't solve by my self.
I have a Pause Button which need to activate pause panel on scene, but nothing work.
1. I have a public GO "Panel" attached in the inspector.
2.Inspector writes that: "There is no 'GameObject' attached to the "Panel" game object, but a script is trying to access it."
3.Script on always active GO.
4.At start Panel is Active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonController : MonoBehaviour {
private Scene ActiveScene;
private GameController gm;
public GameObject panel;
// Use this for initialization
void Start ()
{
gm = GetComponent<GameController>();
ActiveScene = SceneManager.GetActiveScene();
panel.SetActive(false);
}
public void Pause()
{
Debug.Log("Pause");
panel.SetActive(true);
Time.timeScale = 0;
}
public void Menu()
{
SceneManager.LoadScene(0);
}
public void Restart()
{
SceneManager.LoadScene(ActiveScene.buildIndex);
}
public void Play()
{
Time.timeScale = 1;
panel.SetActive(false);
}
Glad if u can help!
I solved the problem: I attached the script twice.

Categories

Resources