Search Unity

Network Tutorial Issue...

Discussion in 'Community Learning & Teaching' started by Steveuk87, Mar 10, 2016.

  1. Steveuk87

    Steveuk87

    Joined:
    Feb 16, 2016
    Posts:
    3
    guys got an issue....

    currently following the unity tutorial for networking a game... i have copied and pasted the code as they have said but the code is giving me errors and i have no idea why...

    the tutorial is here: https://unity3d.com/…/multipla…/adding-multiplayer-shooting…

    and the code i have is:

    using UnityEngine;
    using UnityEngine.Networking;

    public class PlayerController : NetworkBehaviour
    {
    public GameObject bulletPrefab;
    public Transform bulletSpawn;

    void Update()
    {
    if (!isLocalPlayer)
    {
    return;
    }

    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

    transform.Rotate(0, x, 0);
    transform.Translate(0, 0, z);

    if (Input.GetKeyDown(KeyCode.Space))
    {
    CmdFire();
    }
    }

    // This [Command] code is called on the Client …
    // … but it is run on the Server!
    [Command]
    void CmdFire()
    {
    // Create the Bullet from the Bullet Prefab
    var bullet = (Rigidbody)Instantiate(
    bulletPrefab,
    bulletSpawn.position,
    bulletSpawn.rotation);

    // Add velocity to the bullet
    bullet.velocity = bullet.transform.forward * 6;

    // Spawn the bullet on the Clients
    NetworkServer.Spawn(bullet);

    // Destroy the bullet after 2 seconds
    Destroy(bullet, 2.0f);
    }

    public override void OnStartLocalPlayer ()
    {
    GetComponent<MeshRenderer>().material.color = Color.blue;
    }
    }
     
  2. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    I've made video tutorials of those text tutorials and you can see how I've adjusted the code a bit so that it works fine
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,434
    i used this for now, instantiate line was giving errors..

    Code (CSharp):
    1.     void CmdFire()
    2.     {
    3.         // Create the Bullet from the Bullet Prefab
    4.         var bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as GameObject;
    5.  
    6.         // Add velocity to the bullet
    7.         bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
    8.  
    9.         // Spawn the bullet on the Clients
    10.         NetworkServer.Spawn(bullet);
    11.  
    12.         // Destroy the bullet after 2 seconds
    13.         Destroy(bullet, 2.0f);
    14.     }
     
    Akusan likes this.
  4. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    All good then
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This was a typo in the code snippet. IIRC, if you'd followed that steps, but not just pasted the code, it would have been correct. The "final" script has been updated.
     
  6. Akusan

    Akusan

    Joined:
    Aug 24, 2014
    Posts:
    12
    mgear, thanks! you are my hero.

    @Unity, Please fix tutorial?
     
  7. IronmanXXX

    IronmanXXX

    Joined:
    Aug 7, 2013
    Posts:
    11
    Hello,

    I'm going through the tutorial and I get to section 10 and see:
    "
    • ... add the Bullet prefab to the Bullet Prefab field in the PlayerController component.
    • ... add the Bullet Spawn child to the BulletSpawn field in the PlayerMovement component.
    "
    I do not see a PlayerMovement component.
     
  8. IronmanXXX

    IronmanXXX

    Joined:
    Aug 7, 2013
    Posts:
    11
    As soon as I made that post I figured it out.
    Under the PlayerController component there is a Bullet Spawn field. I had been trying to click on it and select my Bullet Spawn child. Finally I tried dragging and dropping the Buller Spawn child into the Bullet Spawn field and it worked. Just clicking on the Bullet Spawn field doesn't give you the option to select anything.



    "... add the Bullet Spawn child to the BulletSpawn field in the PlayerMovement component."
    should be
    "... drag the Bullet Spawn child into the Bullet Spawn field in the PlayerController component."