Adding scripts to gameobjects at runtime with PUN - unity3d

I have a coop game that I'd like to extend to online multiplayer. My game has a series of minigames, sort of like mario party but in a single level. My current code adds and removes scripts from the player objects at runtime depending on the current minigame. For example, when the Tag game loads, it automatically adds the TagPlayerScript to each player, and then removes it once the game ends.
The way I manage this is with a single GameEngine class that iterates through each player in a list.
With Photon, I can't figure out how to make this functionality work. I can instantiate a player prefab for each player, but if I add a script during runtime it only does so on one player's version of the game. How can I make it update on every player's game?

Related

Realtime Sharing of Pointer/Cursor of Each Player in Game Room Using PUN2/MRTK2 on Hololens 2

Is there a way to share each player's raycast pointer/cursor with the other players in realtime? I'm using PUN2 and MRTK2 and Hololens 2.
Approaches I've tried so far:
I tried a naïve approach of modifying the MRTK provided ShellHandRayPointer to contain a PhotonView and then using that on the MixedRealityToolkit object within the scene, but that seems to have no effect.
I've tried creating a cursor prefab based off of MRTK's CursorFocus in which I add a Surface Magnetism component (tracking the hands) and then instantiate this prefab for each player in PUN2's OnJoinedRoom callback. After the instantiation call, I add the object to a non-rendered layer for the player with the goal of hiding it for the local player but allowing it show up for other players. This seems to hide the object as expected when only one player is in the room, but when a second player joins, the first player then sees a cursor show up that tracks with their hand movement, which seems unexpected to me (of note is that I'm using one Hololens 2 headset with a computer acting as the second player). Though perhaps this "crossed" behavior is due to the Surface Magnetism component?
Thanks!
Step-by-step images of how I modified the ShellHandRayPointer with a PhotonView and then reattached to the MRTK system:
scene:
MRTK system:
MRTK system part 2: reference to cloned ShellHandRayPointer:
my cloned ShellHandRayPointer part 1:
PhotonView components expanded on the cloned ShellHandRayPointer:
Regarding how to share objects in Photon in real time, as far as I know, the real-time shared objects in Photon need to be instantiated by PhotonNetwork.Instantiate, but ShellHandRayPointer in MRTK is instantiated by Input system.
You can customize a copy of ShellHandRayPointer, map the position and rotation of ShellHandRayPointer to the copy at runtime and share this copy in Photon in real time.
The position and rotation of ShellHandRayPointer can be obtained in MixedRealityToolkit.InputSystem.DetectedInputSources. Or you can use Unity's methods to get this Game Object directly.
For the cursor, you can use the same method above to create a copy of the cursor and map its position and rotation.

Unity best practice: How to represent (hold and pass) the game items/enemies/attacks/etc? as scriptable-objects or prefabs?

I want to create a game with tons of different enemies, items, accessories, heroes, attacks and more.
To make it easily expandable and maintenance, I want to create the game in the most modular way possible so nothing in the game will be built-in and every aspect of the game will be assembled and configured.
But I'm undecided as to how the elements should be modeled:
As ScriptableObjects - (for example) every enemy will be configured and represented by some scriptabe-object and this asset will contains the enemy stats and behaviours but also the enemy prefabs so the script will tell us how to create the enemy object in the game.
so if I have some place that I want to put some enemy in it, I should (for example) attach on the inspector the enemy's scriptable-object.
As Prefabs - every enemy will be represented by some prefab and the prefab root object will contains some scriptable-object that its purpose is to hold some specific enemy stats and behaviours and the enemy will be act according to them.
so if I have some place that I want to put some enemy in it, I should attach on the inspector the enemy's prefab.
Is there some best practice for that? a way that will make my life easier when the game will be particularly large and complex?

Why is the NetworkIdentity component making my Game Object invisible? (Unity)

Currently trying to make a multiplayer snake game, I have never made a multiplayer game before. I am having a very strange issue where whenever I add the NetworkIdentity component to my 'Snake' game object, it becomes invisible, but is still there. The game is still functional; you just can't see the snake.
I have two pictures attached, one is the game with the NetworkIdentity component, one is the game without it. Thank you for the help.
Without component
With component
A) your image labels seem to be flipped .. exchange the Without and With ;)
and B) Afaik this is the expected behavior for any GameObject with a NetworkIdentity as long as you are not connected to any network yet. It will get enabled once you host or join a session.
You probably would rather convert this into the player prefab though and not have it in your scene from the beginning but rather let the NetworkManager automatically spawn it for every player in the session including yourself.

Unity Photon Player Instantiation

I have a SteamVR Unity project which I'm converting to multiplayer.
When another client joins the game, instead of two different players seeing each other, each player has it's own version of the game where he controls all fo the player instances.
For example, while one player is connected everything is fine, but when a second player joins, the game just adds another Player prefab which the first player controls as well.
I tried replacing the Player with a simple cube and everything seems fine.
both the Player and the cube have Photon Transform View and Photon View scripts.
I would appreciate any help I can get.
This is a common problem, when you start with PUN. You probably setup a player prefab with network synchronization and instantiate that for each player. All scripts on the instances will act on the local input, which is what you see now.
You want two variants of the prefab, for local and remote representation. As it's impractical to always configure two prefabs, instead you build one which initializes itself (in Awake or Start) as local or remote. Your scripts should check the object's PhotonView if it's .isMine or not.
This can be done per Component (in each distinct script) or you could add a component which enables/disables the scripts on a GameObject, depending on isMine.
The Basics Tutorial does this, for example.
Unity doesn't know if it's multiplayer or not. When you give an input all of the scripts who are waiting for input takes it and behaves accordingly. To solve this basically create another player that doesn't take any input and spawn it for other players.

Issue with character models in unity

I am using the Unity 4 game engine and I have two scripts, one that tells the enemy to attack the player, and one that lets the player attack the enemy. The enemy can attack the player, but the other script does not work. If i apply the script to a game object such as a capsule or cube, the script works fine, but not when i apply it to an imported character model. The script basically works by checking the enemy's health and destroying the gameobject if it is equal to or below zero. I have a feeling it has to do with the character model not being an actual game object but i am pretty sure it is. Can anyone help?
When you make the enemy character. Make an empty game object. Then create your enemy inside of that object. In the hierarchy view it should be parented by the empty object. You can also do this by dragging the enemy to the empty game object in the hierarchy view. Then attach the script to the empty game object instead of the model.
Solution
Please check all the tags you are applying to the scripts .No Script could able to work until unless the tagging is done . The tagging help the game object to differentiate between normal object and the enemy so the tagging plays a vital role to run scripts .