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 TobiasAndrion · Jul 28, 2016 at 10:06 AM · networkingmultiplayer-networkingspawning

can't get clients to spawn object

I know there must be something I am missing. I have tried countless different solutions to this problem, but I cannot seem to get my Client players to spawn rockets in this first person game I am working on. I have seen countless similar questions and tried every little piece of advice I could find but with no success. I will start with the shoot rocket script as that is where my command method is located. I have this script attached to every player. The host player can spawn object just fine, but my clients just can't get these darn rockets to spawn.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class ShootRocket : NetworkBehaviour {
 
     public float speed = 10.0f; // how fast rocket shoots
     public GameObject redRocket; //prefab of rocket object
     public GameObject blueRocket;
     //Variables relating to the score ball
     public GameObject scoreBallPrefab; //The object that will be spawned when we let go of the right mouse button
     public Slider chargeSlider; // the slider that is associated with shooting the basketball.
     public float chargeSpeed = 10.0f; //how fast to fill up bar for basketball shot.
     public float maxForce = 350.0f; 
     public Transform bulletSpawn;
     public GameObject myCamera;
 
     [Command]
     public void Cmd_ShootARocket(){
         if(isLocalPlayer)
         {
             if(gameObject.layer == LayerMask.NameToLayer("BlueTeam"))
             {
                 GameObject tempRocket = Instantiate(blueRocket, 
                                                     myCamera.transform.position, 
                                                     Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
                 //NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
                 NetworkServer.Spawn(tempRocket);
             }
             else{
                 GameObject tempRocket = Instantiate(redRocket, 
                                                     myCamera.transform.position, 
                                                     Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
                 //NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
                 NetworkServer.Spawn(tempRocket);
             }
         }
     
     }
 
     // Update is called once per frame
     void Update () 
     {
         if(!base.isLocalPlayer)
         {
             return;
         }
 
         if( Input.GetButtonDown("Fire1"))
         {
             Cmd_ShootARocket();
             
         }
 }

As far as I can tell, this is what everyone is recommending I do. I have tried changing my spawn method to spawnwithplayerauthority which has also failed me miserably. I am under the impression that this is the only script where the problem would be, but I will add a few more likely suspects below just in case. First, my player setup script which is ripped directly from brackeys besides some team changing nonesense I was working on today (I plan on cleaning that up in the near future) using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI;

 /**
  * Initial setup of player characters. Makes sure unnecessary components are turned off.
  * Sets up the players in their appropriate team etc.
  */ 
 public class PlayerSetup : NetworkBehaviour {
 
     [SerializeField]
     Behaviour[] componentsToDisable;
     Camera sceneCamera;
     public GameObject hudObj;
 
     [SyncVar(hook = "OnColorChange")]
     public Color myColor;
 
     void Start () {
 
         //Logic to turn off unnecessary components
         if(!isLocalPlayer)
         {
             for(int curComponent = 0; curComponent < componentsToDisable.Length; curComponent++)
             {
                 componentsToDisable[curComponent].enabled = false;
             }
         }else
         {
             sceneCamera = Camera.main;
             if(sceneCamera != null)
             {
                 sceneCamera.gameObject.SetActive(false);
             }
             GameObject hudInstance = GameObject.Instantiate(hudObj) as GameObject;
             GetComponent<ShootRocket>().chargeSlider = hudInstance.GetComponentInChildren<Slider>();
         }
     }
 
     void Update()
     {
         GetComponent<Renderer>().material.color = myColor;
         if(myColor == Color.red)
         {
             gameObject.layer = LayerMask.NameToLayer("RedTeam");
         }
         else{
             gameObject.layer = LayerMask.NameToLayer("BlueTeam");
         }
     }
 
     public void OnColorChange(Color newColor){
         myColor = newColor;
     }
 }
 

Finally, I will include my extended network manager because it is something I know less about in comparison to other topics using UnityEngine; using System.Collections; using UnityEngine.Networking;

 public class NetworkExtension : NetworkManager {
     public Transform testSpawnPnt;
     public int userCount = 0;
 
     public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
     {
 
         if(userCount % 2 == 1)
         {
                 GameObject player = (GameObject)Instantiate(playerPrefab, testSpawnPnt.position, Quaternion.identity);
                 player.layer = LayerMask.NameToLayer("BlueTeam");
                 player.GetComponent<PlayerSetup>().myColor = Color.blue;
                 NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
         }
         else
         {
             GameObject player = (GameObject)Instantiate(playerPrefab, testSpawnPnt.position, Quaternion.identity);
             player.layer = LayerMask.NameToLayer("RedTeam");
             player.GetComponent<PlayerSetup>().myColor = Color.red;
             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
         }
         userCount ++;
 
     }
     public override void OnServerDisconnect (NetworkConnection conn)
     {
         base.OnServerDisconnect (conn);
         userCount --;
     }
 
 }

Hopefully someone out there knows what the heck is going on. I can provide copious extra details if need be. Please help!

Thanks so much, Tristan

EDIT: I would like to point out that yes, I have registered both of the rocket prefabs and yes they both have network identity and network transform components.

Comment
Add comment · Show 2
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 TobiasAndrion · Jul 28, 2016 at 06:35 PM 0
Share

Okay so Quick Update! I woke up this morning and realized that once the command starts running, the only thing that is running it is the server. Therefore, my problem was that I was checking for isLocalPlayer to be true in my command which would obviously always come up false! $$anonymous$$y command function now looks like this. However, I have now run into a problem that I had earlier which is that when I shoot a client as host, they are not being affected by the host's rocket on either instance! Very confusing but I will update again if I find the culprit.

 [Command]
     public void Cmd_ShootARocket(){
             Debug.Log("$$anonymous$$ade it to command");
             if(gameObject.layer == Layer$$anonymous$$ask.NameToLayer("BlueTeam"))
             {
                 GameObject tempRocket = Instantiate(blueRocket, 
                                                     myCamera.transform.position, 
                                                     Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
                 //NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
                 Debug.Log ("Created tempRocket");
                 Debug.Log (tempRocket.ToString());
                 Debug.Log ("is Client comes back as" + isClient);
                 NetworkServer.Spawn(tempRocket);
             }
             else{
                 GameObject tempRocket = Instantiate(redRocket, 
                                                     myCamera.transform.position, 
                                                     Quaternion.LookRotation(myCamera.transform.forward)) as GameObject;
                 //NetworkServer.SpawnWithClientAuthority(tempRocket, connectionToClient);
                 Debug.Log ("Created tempRocket");
                 Debug.Log (tempRocket.ToString());
                 Debug.Log ("is Client comes back as" + isClient);
                 NetworkServer.Spawn(tempRocket);
             }
     
     }
avatar image Ma_ti TobiasAndrion · Jul 28, 2016 at 09:00 PM 0
Share

What do you mean by client not being affected by rocket? Does it have some script attached to work?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why are objects only spawning on host but not on remote clients? 3 Answers

Check when a player finished spawning [Netcode For GameObjects!] 2 Answers

Change player object - UNET 1 Answer

How to find gameobject through SpawnMessage? 0 Answers

Camera not spawning properly on network 0 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