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 UnityNoob123 · Feb 21, 2017 at 12:00 AM · networkingienumerator

Ienumerators not being called on client?

Hey so I made a Ienumerator to have a spell cooldown, which works great on the lan host client, but anyone who joins the game can only shoot once and it seems that the reset Ienumerators never get run. I will link my script down below any help is appreciated!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class shootSpell :  NetworkBehaviour {
     
     public Camera cam;
     public GameObject bulletPrefab;
     public GameObject bulletPrefab2;
     public GameObject bulletPrefab3;
     public Transform bulletSpawn;
     public Transform bulletWallSpawn;
     public NetworkAnimator netAnim;
     bool canShootMain = true;
     bool canShootAlt = true;
     bool canShootE = true;
     public Image crossHair;
     public Image altIcon;
     public Image eIcon;
     // Use this for initialization
 
     void Start () {
         netAnim = GetComponent<NetworkAnimator> ();
     }
 
     // Update is called once per frame
     void Update () {
         if (!isLocalPlayer) {
             return;
         }
             if (Input.GetButton ("Fire1")) {
                 if (canShootMain == true) {
                     canShootMain = false;
                     StartCoroutine (spellMainDelay ());
                     crossHair.fillAmount = 0;
                 }
             }
             if (Input.GetButton ("Fire2")) {
                 if (canShootAlt == true) {
                     canShootAlt = false;
                     StartCoroutine (spellAltDelay ());
                     altIcon.fillAmount = 0;
                 }
             }
             if (Input.GetKey ("e")) {
                 if (canShootE == true) {
                     canShootE = false;
                     StartCoroutine (spellEDelay ());
                     eIcon.fillAmount = 0;
                 }
             }
     }
 
     void FixedUpdate () {
         if (canShootMain == false && crossHair.fillAmount < 1) {
             crossHair.fillAmount += 0.0165f;
         }
         if (canShootAlt == false && altIcon.fillAmount < 1) {
             altIcon.fillAmount += 0.0013f;
         }
         if (canShootE == false && eIcon.fillAmount < 1) {
             eIcon.fillAmount += 0.002f;
         }
     } 
 
     [Command]
     void CmdShoot1 () {
             var bullet = (GameObject)Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
             bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 10;
             NetworkServer.Spawn (bullet);
             Destroy (bullet, 5f);
             StartCoroutine (mainSpellCooldown ());
     }
 
     [Command]
     void CmdShoot2 () {
         var bullet = (GameObject)Instantiate (bulletPrefab2, bulletWallSpawn.position, bulletWallSpawn.rotation);
         bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 10;
         NetworkServer.Spawn (bullet);
         Destroy (bullet, 5f);
         StartCoroutine (altSpellCooldown ());
     }
 
     [Command]
     void CmdShoot3 () {
         var bullet = (GameObject)Instantiate (bulletPrefab3, bulletSpawn.position, bulletSpawn.rotation);
         bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 10;
         NetworkServer.Spawn (bullet);
         Destroy (bullet, 5f);
         StartCoroutine (ESpellCooldown ());
     }
 
     IEnumerator spellMainDelay () {
         netAnim.SetTrigger ("rightSwing");
         yield return new WaitForSeconds (.2f);
         CmdShoot1 ();
     }
 
     IEnumerator mainSpellCooldown () {
         yield return new WaitForSeconds (1f);
         canShootMain = true;
     }
 
     IEnumerator spellAltDelay () {
         netAnim.SetTrigger ("Earthquake");
         yield return new WaitForSeconds (.1f);
         CmdShoot2 ();
     }
 
     IEnumerator altSpellCooldown () {
         yield return new WaitForSeconds (15f);
         canShootAlt = true;
     }
 
     IEnumerator spellEDelay () {
         netAnim.SetTrigger ("DownHit");
         yield return new WaitForSeconds (.1f);
         CmdShoot3 ();
     }
 
     IEnumerator ESpellCooldown () {
         yield return new WaitForSeconds (10f);
         canShootE = true;
     }
 }
 
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 James2Games · Feb 21, 2017 at 04:23 AM 1
Share

There's an issue with Enumerators where sometimes they can't modify non-static fields so if you debug you might find the variables aren't changing back to true. I don't know a fix for this unfortunately but a workaround is to make them all static if there is only one "shootSpell" per game which seems unlikely.

avatar image UnityNoob123 James2Games · Feb 21, 2017 at 05:09 AM 0
Share

Thanks for letting me know, I started to search around for other ways and came across the Time.time feature. In case anyone stumbles upon this in the future this link saved my game. https://docs.unity3d.com/ScriptReference/Time-time.html

1 Reply

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

Answer by DominikHeyn · Feb 22, 2017 at 10:14 AM

Hey there, I can't fix your problem without playing around with it but I'd like you to introduce to the way I handled cooldowns and stuff:

     [ClientCallback] void Update () {
         if (!isLocalPlayer)
             return;
 
         bool cooldownComplete = (Time.time > nextReadyTime);
         if (cooldownComplete) {
             if (Input.GetButton("Fire1")) {
                 AbilityCooldownMechanics ();
                 // Call here your method for casting the spell
             }
         } 
         else {
             OnCooldown ();
         }
     }
 
     private void OnCooldown() {
         cooldownTimeLeft -= Time.deltaTime;
         float roundedCD = Mathf.Round (cooldownTimeLeft);
     }
 
     private void AbilityCooldownMechanics() {
         nextReadyTime = cooldownDuration + Time.time;
         cooldownTimeLeft = cooldownDuration;
     }

Maybe you can use this so you don't have to deal with your current problem. :-)

Dominik

Comment
Add comment · Show 2 · 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 UnityNoob123 · Feb 22, 2017 at 11:15 PM 0
Share

Thanks for making a thoughtful input but I did manage to get around this issue using that Time.time feature you brought up. If you are knowledgeable about Unet and are willing to glance at a problem ive had for the past month, I would greatly appreciate it! If not that's totally ok thanks for your input on this question. http://answers.unity3d.com/questions/1316790/unet-igonore-collisions-between-layers.html

avatar image DominikHeyn UnityNoob123 · Feb 23, 2017 at 06:01 PM 0
Share

I'll check this out! :-)

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

87 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

Related Questions

Ienumerator not working on client side? 0 Answers

Check every 3 seconds if the server side processing is done 0 Answers

Spawn Objects Inside IEnumerator In Serverside (Unity Networking) 1 Answer

Why is this method being called multiple times? 0 Answers

Unity networking tutorial? 6 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