Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 normand · Apr 28, 2016 at 08:47 AM · playerrandomplayerprefs

NetWork: Random Player

HI, I try to spawn a different playerprefab each time the client connect the server. So, basicly, I try this just to teste if the prefab have changed befor script the random part. ex:

using UnityEngine. using UnityEngine.Networking; public class PlayerPrefManager : NetworkManager {

 public GameObject playerPrefab;
 public GameObject playerPrefab2;

 public override void 

OnServerReady(NetworkConnection conn) {

     playerPrefab=playerPrefab2;
 }

}

But nothing happen. How can I rosolve this whitout change anything to my playerprefab because it is done.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheSorm · Apr 28, 2016 at 10:24 AM

You can just override this methode:

 // called when a new player is added for a client
 public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
 {
     var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
     NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
 }

And put your different players randomly.

Comment
Add comment · Show 11 · Share
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 normand · Apr 29, 2016 at 11:15 PM 0
Share

Thanks for your answer, but it's not usefull. Where did you take your "playerSpawnPos". Where the "var player'' comme from? You script in C-sharp and changed to Java and reScript in C-Sharp ?? So' I'll put my code and maybe it will make more sens to you. :)

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

So bassicly it's what I try to do. Change the playerPrefab to my playerPrefab(number).

Thanks for your help .

avatar image TheSorm · Apr 30, 2016 at 06:16 PM 0
Share

no this is the official OnServerAddPlayer function from Unity, this is the funktion you have to override (Same like you do with OnServerReady) and put your player prefab as "playerPrefab".

avatar image normand TheSorm · May 01, 2016 at 12:16 PM 0
Share

Okey!!! I don't want to be rude because the english it's not my first language but...Did you try your code before send your answer?? I put a Debug.Log("Good");in your line of code and nothing happen. Also, some error come from your Instatiate transform.position. Of course I changed it for Vector3.zero but...I stay with the fact that the Debug.Log(""); Show me nothing in the console. So basacly, can you please, tell me what is your varible player??? From where did you get this variable?? Did I need to change var player for playerPrefab??? Where I put my variable playerPrefab2 , playerPrefab3, playerPrefab4.....etc?? And you use Instantiate and why not network.Instantiate?? Please, if your not abble to answer correctly, don't post anymore. I want to learn and not to be confuse and turning arround it too long . I need to progress... I anderstand that you try to help and tell me that is in the OnServerAddPlayer function then i need to put my code...but where and how?????? It's too longue time I try and try and try. Almost a week. So, I'm not asking for nothing because I tryed a lot code and no result. It's for this reason then I come here. And also I'm not the only one who is trying and asking for that easy answer. like I said, I don't want to be rude. It's not a forum. But when I try something that some one posted and I have error on debug or not ennought concreat explication. It's p*** me.....

Thanks again,

avatar image TheSorm · May 01, 2016 at 02:12 PM 0
Share

Sorry but if you don't understand my anser try out unity tutorials on the tutorial page . Unity Network is advance work and you have to know about override methods and accesing the right scripts. I cut post you a functional script but I don't think you would learn how to do it just give you the functional code

avatar image normand TheSorm · May 01, 2016 at 09:07 PM 0
Share

You talk about override, but you use virtual. And if I'll use override. It make and bigggg error on the console. And like I said, the debug.log show nothing when the host is connect or the client. Amd yessssss I will learn whith the entire code because I'll use it for other thing and not be stuck. If people don't show how or explain how to do it or in details like . Ex : Ho, this is for this, that it's for that. You should do this for that, and that for this. The only way I figure out How to do this it's like this.... I put my different player in an Empty gameObject and in the Start() I'll say if randomNumber it's 1, close the player 2,3,4 and detatch me from my parent. But if I"ll try that way, I'll will need to restart my entire game. And I'm asking a way without change enything on my fisnished game.

avatar image TheSorm · May 01, 2016 at 09:23 PM 1
Share
 public class PlayerPrefmange : Network$$anonymous$$anager {
     public GameObject playerPrefab1;
     public GameObject playerPrefab2;
     public GameObject playerPrefab3;
     public GameObject playerPrefab4;
     Vector3 playerSpawnPos = new Vector3(1,1,1);
     int randomNumber;
     // Use this for initialization
     void Strat() {
 
         randomNumber = Random.Range(0, 4);
 
     }
 
     public override void OnServerReady(NetworkConnection conn) {
 
         if (randomNumber == 0) {
             Debug.Log("playerPrefab");
             playerPrefab = playerPrefab;
         }
         if (randomNumber == 1) {
             Debug.Log("playerPrefab1");
             playerPrefab = playerPrefab1;
         }
         if (randomNumber == 2) {
             Debug.Log("playerPrefab2");
             playerPrefab = playerPrefab2;
         }
         if (randomNumber == 3) {
             Debug.Log("playerPrefab3");
             playerPrefab = playerPrefab3;
         }
         if (randomNumber == 4) {
             Debug.Log("playerPrefab4");
             playerPrefab = playerPrefab4;
         }
 NetworkServer.SetClientReady(conn);
     }
 
     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
         GameObject player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
     }

You override the OnServerReady function and you have to call " NetworkServer.SetClientReady(conn);" than you override "OnServerAddPlayer" so you can set what player prefab is loadet for the player spawning.

Soory i cant test this but i hope you understand whats up in this code if not just look at http://docs.unity3d.com/$$anonymous$$anual/UNet$$anonymous$$anager.html and learn how to edit the Network player functions.

avatar image normand TheSorm · May 02, 2016 at 02:40 AM 0
Share

Thanks a lot!!!! Thats my boy!!! It doesn't work on a first try but after play around it. I found the solution easly. So basicly, you gived to me the right call thath I missed. NetworkServer.SetClientReady(conn);Also I change the player variable for playerPrfab and change the playerPrefab in the Instantiate for my custom Player1,2,3,4etc... So with this I made my script that way..... :)

 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 
 public class PlayerPrefmange : Network$$anonymous$$anager {
      public GameObject playerPrefab;
     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
 
     public List<GameObject> spawnPrefabs;
 
     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(playerPrefab1, Vector3.zero, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);        }
         if (randomNumber == 1) {
             Debug.Log("playerPrefab1");
             playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab1, Vector3.zero, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);        }
         if (randomNumber == 2) {
             Debug.Log("playerPrefab2");
             playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab2, Vector3.zero, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);        }
         if (randomNumber == 3) {
             Debug.Log("playerPrefab3");
             playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab3, Vector3.zero, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);        }
         if (randomNumber == 4) {
             Debug.Log("playerPrefab4");
             playerPrefab = (GameObject)GameObject.Instantiate(playerPrefab4, Vector3.zero, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);        }
     }
 }

So, everytime when some one is connected, the playerPrefab change for any player then I made. Now I need to researsh for the net work StartPosition. :P So sorry to was maybe rude...but now I leraned a lot and can work forward to use this methode for otherthings. :) 2 thumbs up......!!!!

avatar image normand TheSorm · May 02, 2016 at 04:01 AM 0
Share

Hummm... the random player work great but it give me an other big problem. Now, just the host can see all player and effect. I think it's because it's not a Network.Intantiate. but, if I try playerPrefab = (GameObject)Network.Instantiate(playerPrefab3, Vector3.zero, Quaternion.identity,0); it tell me

  Failed Network.Instantiate because we are not connected.
     UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
     PlayerPrefmange:OnServerAddPlayer(NetworkConnection, Int16) (at Assets/PlayerPrefmange.cs:54)
     UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

So I dont know what to do but I'm close...I know!

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

Hooooo, I found my error!!!! I didn't put the other custom player in the SpawnInfo. it was for that reason only the host was able to see other player. So we don't need to call Network.Instanciate. Thanls you so much. I'll be able to help other french people... Again, 2 Thumbs Up!!

Show more comments
avatar image
1

Answer by MakakWasTaken · Apr 30, 2016 at 07:32 PM

Can't you make an array of GameObjects and then use Random.range(0,array.Length) to select the index and then use the override that @TheSorm posted to spawn that specific prefab

Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to disable writing to Windows registry? 0 Answers

playerprefs 0 Answers

Why is instantiate and playerprefs not working when combined? 1 Answer

Spawning a player when loading new level 1 Answer

Questions about changes to UnityEngine.Random. Random.Seed deprecated 3 Answers


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