How to spawn prefab from instantiated object Mirror Unity - unity3d

I'm new to Unity's Mutliplayer system and I don't really know how to do a specific thing.
Players can have multiples weapons. When the player change weapon, an object is instantied from the resources only client-side. The thing is that a weapon needs to spawn object(s), with reference(s) and use them after. I made all weapons inherit from NetworkBehaviour and tried to call [Command] methods or [ClientRpc] method but get many errors or it just worked for one player.
The only thing I did well was to take a weapon code and execute it from a player script but move all the code from all weapons to a script on the player doesn't make any sense.
I know now that I can't call [Command] from my weapons so how I'm supposed to make those things work with the server ?

Related

Issue with parenting networked objects (Unity Mirror)

So I've been making this online survival game using mirror and I have come across an issue with spawning in a weapon for the player.
Apparently you can only parent an object to another networked object (when executed by the server).
When I was doing this in single player I had no issues obviously as I could just set the parent to my camera holder game object and it will follow the cameras rotations as expected.
Now for the client to see the other players spawned equippable, I can only seem to set the parent of the equippable to the player transform where the network Identity/transform is.
If I go to set it to the camera holder then nothing happens, the equippable will just spawn in at 0,0,0.
So with this said, is this the only way to do this? Have a ClientRpc tell the client to parent the equippable to the root of the player and then have code on the equippable to follow the camera?
I feel like I'm missing something important as this method seems quite finniky.
Thanks in advance.
I have tried putting another network transform on the gameobject I would like the parent to be set.. mirror doesn't like this, was hoping it would parent but it just goes to 0,0,0
I have tried to code moving the equippable with the camera but I wasn't getting good results so I thought I would ask here if there was a much easier and or practical way to execute this.

A* (Astar) Pathfinding doesn't work with Player Clone / Instantiate Unity

My player spawns at game play.
when i put my Player Prefab into "target" then it doesn't follow.
but when i put my clone(instance) of my prefab (what spawned in the hierachy when i start the game) into "target" then it works.
how can i do that the target get automatically the instance of my player prefab? i was thinking about anything with "find game object with tag" but i'm legit a c# noob, i prefer using bolt. only for the pathfinding i need to use that way.
One option is to use the function GameObject.Find. E.g. GameObject.Find("Player"); or GameObject.Find("Player(clone)");. This will find the GameObject with that name. Here is the documentation for it: https://docs.unity3d.com/ScriptReference/GameObject.Find.html

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.

Unity, to what object should I attach my game engine script

Me and my friend are building a simple idle game as our first project in unity.
We got to the point at which we build a script that handles all calculations, and communicates with each GameObject the player sees.
Now, to what should we attach this script?
We would rather not use a GameObject because:
1) The GameObject would be an overkill
2) The GameObject lies in the space where all of my "physical" objects exist (That would be useless since the script does not have to "exist somewhere")
First time posting in stackoverflow, if I made any mistake please tell me ^^
Convention says to put it on the main camera or an empty GameObject.
If you really don't want to do either of those, you could make your class static.
If your class inherits from MonoBehaviour you have to attach it to a GameObject. If you want to attach it to a GameObject it has to inherit from MonoBehaviour.
If you want to better understand the Unity way, I advise you to read up on Entity-Component Systems:
GameObject are Entities
MonoBehaviour are Components
Services and managers generally get implemented as Components in Unity which then get attach to the main camera or to empty GameObjects named after them.
You can create a usual singleton class, and "Game Initializer" monobehaviour, that will initialize all your singletone managers, fabrics, etc...
Just add this monobehaviour to every scene in empty gameobject with a code like that:
if(GameManager.instance == null)
new GameManager();
Or in case of scriptableObjects,
if(GameManager.instance == null)
gameManager.init()
Also, a good desicion is to use Entity System pattern, here is frameworks for unity (Unity-Ash, Entitas, etc)