Unity Photon Sync Coins for all Players - unity3d

I have an problem on Synchronizing an Integer.
When I collect with the first Player (The First Connected with the room) a Coin Than the Text(UI) of the Coins are Changing on all Clients but if I collect with an other Player it Only Change for hisself.
How Can I fix this Problem?
This is the Code for Synchronizeing the Coins:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoneyManager : MonoBehaviour{
public int Coins;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.isWriting){
stream.SendNext(Coins);
}else if(stream.isReading){
Coins=(int) stream.ReceiveNext();
}
}
}
This is the Code for changing The Text of the UI Element
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UiManager : MonoBehaviour{
public Text Coins;
void FixedUpdate(){
Coins.text=" Coins: "+GameObject.Find("MoneyManager").GetComponent<MoneyManager>().Coins;
//some Other Code for other Things...
}
}
If you need some other things to know just ask.

Related

The Name 'GUILayout' does not exist in the current context

I'm doing procedural terrain generation in unity from Sebastian Lague's tutorial, and this code isn't working. How can I fix that?
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor (typeof(MapGenerator))]
public class MapGeneratorEditor : Editor {
public override void OnInspectorGUI() {
MapGenerator mapGen = (MapGenerator)target;
DrawDefaultInspector ();
if (GUILayout.Button ("Generate")) {
mapGen.GenerateMap ();
}
}
}
You should also include UnityEngine namespace
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Why is my unity scene script not working?

using a button to load a scene and iam just getting the error "Assets\Scripts\button_press.cs(15,59): error CS1503: Argument 1: cannot convert from 'UnityEngine.Object' to 'string'"
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class button_press : MonoBehaviour
{
public Object sceneToLoad;
public void PlayGame ()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad);
}
public void QuitGame()
{
Debug.Log (" You have quit");
Application.Quit();
}
}
The LoadScene API expects either an integer referring to the scene number in the build, or a string referring to the name of the scene (generally preferred).
You should define your sceneToLoad field as a string rather than an Object.

Unity Text(Legacy) disapears on WebGL build

I am trying to make a FlappyBird on WebGL with Unity. I made scores system, and there is no bug finded while runing on unity editor, but when I build with WebGL, Text don't shows up. Only legacy Text (Not TextMeshPro) causes this problm, so it would be helpful too if there is a way to use TextMeshPro. https://r0k0r.github.io/FlappyBirdWebGL is my game link, and https://github.com/R0K0R/FlappyBirdWebGL is my github link. I don't know this will help, but I am currently coding with ubuntu.
this is my Score.cs code that returns current score:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int score = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(){
score++;
}
public static int returnScore(){return score;}
}
and this is my ApplyScore.cs code that applys score to text gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ApplyScore : MonoBehaviour
{
public Text ScoreText;
// Start is called before the first frame update
void Start()
{
ScoreText.text = "0";
}
// Update is called once per frame
void Update()
{
ScoreText.text = Score.returnScore().ToString();
}
}
this is what it looks like
and this is what it should look like

Keeping track of master volume slider PlayerPref in another scene

I'm quite new to Unity and just started learning UI and made a volume slider in a settings scene. I've learnt how to use those 2 functions to save the PlayerPref of the volume so whenever the player enters the settings scene again, the volume will be saved from last time.
However, I wonder how you can change it so it will do the same but on the main menu? So that way the player doesn't need to go back to settings for it to apply.
The code:
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
public class SettingsMenu : MonoBehaviour
{
public Slider volumeSlider;
void Start()
{
keepVolumeValue();
GameObject.FindGameObjectWithTag("Music").GetComponent<MusicManager>().playMusic();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
SceneManager.LoadScene("MainMenu");
}
private void OnDisable()
{
PlayerPrefs.SetFloat("volume", volumeSlider.value);
}
public void keepVolumeValue()
{
volumeSlider.value = PlayerPrefs.GetFloat("volume", volumeSlider.value);
}
public void setVolume(float volume)
{
mainMixer.SetFloat("volume", Mathf.Log10(volume)*30f);
}`
Ty for any help

NullReferenceException using ScriptableObjects in Unity

I have setup a Scriptableobject in database called "Card" and am attempting to make a searchable list of those items in the scriptableobject. I attached CardDatabase script to an empty gameobject called _CardDatabase. I am not sure if I am attaching it incorrectly or what I have done wrong here.
I am still getting the following error:
NullReferenceException: Object reference not set to an instance of an object
CardDatabase.GetCardByID (System.String abbr) (at Assets/Scripts/CardDatabase.cs:33)
// CardDatabase.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CardDatabase : MonoBehaviour
{
public CardList cards;
private static CardDatabase instance;
private void Awake()
{
if (instance = null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public static Card GetCardByID(string abbr)
{
return instance.cards.AllCards.FirstOrDefault(i=> i.abbr == abbr);
}
}
// CardList.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Cards Database",menuName = "Cards Database")]
public class CardList : ScriptableObject
{
public List<Card> AllCards;
}