How do I sync a script over Photon Network in Unity? - c#

I have created a Game class which handles all transactions of a game (whose turn it is, what items each person has, etc). I would like to create one of these objects and keep it synced between both clients of a Photon Unity network.
First I tried Instantiating a prefab over the Photon network with InstantiateSceneObject() and then changing the attached script. The prefab is then found on both clients, but the attached game script is null on one of the players.
After that I tried adding the Game class to a Hashtable and then calling
PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "game", g } });
But when I attach my debugger to Unity, I notice the script hangs at this line. It doesn't move on to the next line of code.
I am really hoping I don't have to send every object of my game class as an RPC. Seems really tedious. Any help is greatly appreciated.

Related

How do I use trigger to destroy blocks on quest 2 unity?

I am making a game in Unity that is a combination of gorilla tag and minecraft and I am having trouble with using the RawAxsis1D.RIndexTrigger (top right quest 2 controller trigger) to destroy objects. (Some of my objects I'm trying to destroy have children.) I am also using raycasing so that it will make it so it will destroy the object the player is looking at, so far everything I have tried has failed.
Can anyone help me out? I am happy to provide any further info.
Main problem: when I add my script to my player, it did not do anything when the trigger was hit.

How to communicate between 2 scenes in Unity?

I would like to make a color system for my player. In my game scene the player can pick up the coins and the amount of coins will be save with playerprefs , but I don' know how can I use the amount of coins in my menu scene.
And I need some help to a player color selecter too. When the player select a color than in my game scene must instiate the player with the color.
Soo I think , i need to know how to communicate between 2 scenes.
Can somebody help me with some tutorial?
There are multiple ways to achieve this, which I feel comes down to a matter of taste: save data to file, use DoNotDestroyOnLoad...
But from what I understand, the recommended way now is to create a "manager scene" which will stay alive throughout the lifetime of your app and pass data to and from your other scenes as they are opened and closed, instead of using DontDestroyOnLoad:
It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress. src
See the Unity guide here. Basically you would have 2 scenes open at the same time, at any given moment: the manager scene and whatever the actual active game scene is. Then you can communicate between scripts in the two open scenes via event delegates. The way it would work is:
Player selects color in scene1
Color is sent from scene1 to manager scene via event delegate
scene1 is unloaded and scene2 is loaded
Color is sent from manager scene to scene2
This is the approach I've been using for a project now which looks like this:
"0-Attract", "1-Sealant", "2-Paint", and "3-Conclusion" are my actual game scenes, and "Manager Scene" contains everything that exists in every other scene (thus no reason to kill and respawn them) as well as all of my "manager" scripts which handle the passing of data between scenes.
Note that multi-scene editing can be confusing at first, as there are new things you need to pay attention to (i.e., which scene is currently "active" and what that means) so be sure to go through the unity guide I posed above. Good luck!
you can Call DontDestroyOnLoad method on any object that you want that will remain wen you are moving the a new scene so it will not be destroyed when scene is closing.

Destroying all the previously created objects when the Game Start over

In my game I am using Destroy(gameObject) for some gameobjects to pass them on another level. Now the problem is after my player dies there are objects left on the scene when I restart the game after the player dies, I only want the objects that i created when the game started.
How can I accomplish this?
Store the player created gameObjects in a list, then call Destroy() on them after a restart.
First of all if you need to destroy object that you marked with DontDestroyOnLoad you are doing something wrong.
Use it only on objects that you really want to keep across the scenes. If you need to make changes in these objects, just change parameters values and make other objects live only in scenes where they belong.

How to sync data when joining to a photon rooms that created before?

I am trying to build a game like http://slither.io/, using Photon Unity networking. The problem is here that I don't know how to get data from a photon room that was created before. for example, we have a room that already has 10 players and a new player wants to join this room.
I already know that if two or more players are in a room I can use PhotonNetwork.Instantiate() to create some object to share data of that special object with players in the room.
One possible solution that comes to my mind is to create one GameObject for every player who is in the room with a PhotonView component and photon will update data of those objects. Is that OK?
Thanks
This is the answer I got from the Photon forum :
You can store data in room properties, use buffered RPC's, or even get
data from master on joining room.
hope it helps.

Unity is possible to add a Game Object in hierarchy but not instantiate it?

I'm not sure if it is possible to create a Game Object in the scene (so it appears in the hierarchy) but I would like Unity to not instantiate (or rather remove it) such Game Object when I press play.
The reason why I want to do that. I'm creating Game Objects as 'enemy spawners' in my game. But I don't want to have those transforms or Game Objects actually in memory while the game is running. So I've created an editor script that search for all the spawner entities, retrieve the information that I need and creates a binary file with it that later the enemy manager reads in run time during the initialization process.
If this is not possible, do you guys have any recommendations on how I could implement something similar?
Many thanks in advance.
You are looking for scriptable objects.
Here are some URL's
ScriptableObject Tutorial from Unity
ScriptableObject documentation
ScriptableObjects can be accesed at any time and don't have to be instantiated before you can use them. So they won't have a transform.

Categories

Resources