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 /
  • Help Room /
avatar image
0
Question by Topsu · Nov 09, 2017 at 09:53 PM · networkingmultiplayer-networkingobject-oriented-programming

Networking attributes through interface

Hello, I have my WeaponControl script call functions in my weapons through IWeapon interface, but the problem is that I cannot use NetworkServer.Spawn(bullet); on my weapon, even with the [Command] attribute because I get error "NetworkServer is not active. Cannot spawn objects without an active server."

Everything works fine on host side though.

WeaponControl.cs

 public class WeaponControl : NetworkBehaviour {
     // Use this for initialization
     private IWeapon currentWeapon;
 
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         if (!isLocalPlayer)
         {
             return;
         }
         if (CrossPlatformInputManager.GetButtonDown("Fire1" + tag))
         {
             currentWeapon.CmdCharge();
         }
         if (CrossPlatformInputManager.GetButtonUp("Fire1" + tag))
         {
             currentWeapon.CmdFire();
         }
     }
     public void Switched(IWeapon newWeapon)
     {
         Debug.Log("New Weapon");
         currentWeapon = newWeapon;
     }
 }    

IWeapon.cs

 public interface IWeapon
 {
     void CmdFire();
     void CmdCharge();
 }

Gun.cs

 public class GunController : NetworkBehaviour, IWeapon
 {
     public GameObject bulletPrefab;
     public Transform bulletSpawn;
     public double timeToMaxCharge;
     public float minCharge;
     public float maxCharge;
     public double cdAmount;
     private double timeStamp = 0; //cooldown
     private double chargeStart;
     private float charge;
     private bool onCd;
     public int parentIdShow;
     [SyncVar(hook = "CallbackSetOwner")]
     public NetworkInstanceId parentId;
     private WeaponControl weaponControl;
 
     // Use this for initialization
     void Start () {
         weaponControl = gameObject.transform.parent.gameObject.GetComponent<WeaponControl>();
         weaponControl.Switched(this);
         //gameObject.transform.position = gameObject.transform.parent.Find("GunPosition").transform.position;
     }
     [Command]
     public void CmdCharge()
     {
         if (timeStamp <= Time.time)
         {
             onCd = false;
         }
         chargeStart = Time.time;
     }
     [Command]
     public void CmdFire()
     {
         if (!onCd)
         {
             double chargeTime = Time.time - chargeStart;
             double chargePercent = chargeTime / timeToMaxCharge * 100;
             charge = (float)maxCharge / 100 * (float)chargePercent;
 
             if (charge > maxCharge)
             {
                 charge = maxCharge;
             }
             if (charge < minCharge)
             {
                 charge = minCharge;
             }
             Debug.Log(charge);
             // Create the Bullet from the Bullet Prefab
             var bullet = (GameObject)Instantiate(
                 bulletPrefab,
                 bulletSpawn.position,
                 bulletSpawn.rotation, gameObject.transform);
             // Add velocity to the bullet
             bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * charge;
             timeStamp = Time.time + cdAmount;
             NetworkServer.Spawn(bullet);
         }
         onCd = true;
     }
 }
 ...
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

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

154 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How do I implement a message based network system UNITY 5? 0 Answers

How to smoothly interpolate game object positions in a network game? 0 Answers

Assign Network Manager an Online Scene with Script 0 Answers

UNET NetIDs for Scene Objects Mismatched Between Client and Server 1 Answer

How configure a dedicated server about Unet 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