Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by twoface262 · Aug 05, 2015 at 08:15 PM · c#playerspawnspawningunity multiplayer

player spawning

Hello! I need help spawning my player through unet!

I'm using the MatchMaker script provided from the documentation here (Except I'm making it derive from NetworkManager): http://docs.unity3d.com/Manual/UNetMatchMaker.html

And I added my own custom code to spawn the player after the client has connected. The problem I'm facing is that the player will not spawn no matter what. I'm not getting any error messages either -.. It's just not spawning. Why?

Here is my spawn code --

 public void OnConnected(NetworkMessage msg)
 {
     ClientScene.AddPlayer(myClient.connection, 0);
     
     Debug.Log("Connected!");
 }
 
 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
 {
     GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
     Debug.Log("spawn player");
     NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
 }


Any help is greatly appreciated and will get you guys cookies! :)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image imilanspinka · Aug 05, 2015 at 08:37 PM 0
Share

I don't know, but shouldn't the

     NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);

line be the first line in OnServerAddPlayer(...)?

And do you get the Log "spawn player"?

avatar image twoface262 · Aug 05, 2015 at 08:38 PM 0
Share

No, because you must instantiate the object in the server before adding it for the clients.

And oddly, no. I'm not getting the debug message so it's as if the OnServerAddPlayer isn't even being called.

avatar image normand twoface262 · May 02, 2016 at 05:50 AM 0
Share

This is my code for spawning random player. maybe you'll find what you've fogot or missed.

Also, make sure to put your player in your spawn info....

 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 using System.Collections;
 public class PlayerPrefmange : Network$$anonymous$$anager {
     public GameObject playerPrefab1;
     public GameObject playerPrefab2;
     public GameObject playerPrefab3;
     public GameObject playerPrefab4;
 
     int randomNumber ;
     Vector3 playerSpawnPos= new Vector3(1,1,1);
     // Use this for initialization
 
 
     void Strat () {
         randomNumber=Random.Range(0,5);
 
 
     }
     
     // Update is called once per frame
 
     void Update () {
         randomNumber=Random.Range(0,5);
     
     }
 
     public override void OnServerReady(NetworkConnection conn) {
 
         NetworkServer.SetClientReady(conn);
 
     }
 
     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
 
             if (randomNumber == 0) {
                 Debug.Log ("playerPrefab");
             playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab, playerSpawnPos, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
             }
             if (randomNumber == 1) {
                 Debug.Log ("playerPrefab1");
             playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab1, playerSpawnPos, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
             }
             if (randomNumber == 2) {
                 Debug.Log ("playerPrefab2");
             playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab2, playerSpawnPos, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
             }
             if (randomNumber == 3) {
                 Debug.Log ("playerPrefab3");
             playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab3, playerSpawnPos, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
             }
             if (randomNumber == 4) {
                 Debug.Log ("playerPrefab4");
             playerPrefab = (GameObject)GameObject.Instantiate (playerPrefab4, playerSpawnPos, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection (conn, playerPrefab, playerControllerId);
 
         }
     }
 
 
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make background loop and camera follow spawned gameobjects? 1 Answer

How to make my enemies spawn at spawn point, but not randomly? 1 Answer

Instantiate is throwing my throwing my objects? 1 Answer

2D Player Spawn Issue 0 Answers

Enemies spawn on top of each other(C#)(Unity) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges