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 Omerl_ahire · Jan 08, 2017 at 03:24 PM · coroutineother script

Starting Coroutine from another script problem.

Hi, I'm having a problem trying to call an IEnumerator for a power up, I have been able to make my first power up script work and am using an almost exact copy of that code on this script but the co routine is not starting in the other script, any help would be appreciated, Thanks.

Power Up Script.

 using UnityEngine;
 using System.Collections;
 
 public class PowerUp2 : MonoBehaviour {
 
     GameObject powerUp2;
     public  AttackTrigger Buff;
     public GameObject Heavy;
     public GameObject Light;
 
     void Start()
     {
         Buff = Heavy.GetComponent<AttackTrigger>();
         Buff = Light.GetComponent<AttackTrigger>();
         powerUp2 = GameObject.FindGameObjectWithTag("powerUp2");
     }
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             Buff.StartCoroutine("Buff2");
             Destroy(this.gameObject);
         }
     }
 }
 
 ## Script To Be Powered Up. 



 using UnityEngine;
 using System.Collections;
 
 public class AttackTrigger : MonoBehaviour {
 
     [SerializeField] int damageVal;
     [SerializeField] int critVal;
     HealthBloodKnight critFill;
     public GameObject player;
     public bool buffed;
 
     void Start() {
         critFill = player.GetComponent<HealthBloodKnight>();
         buffed = false;
     }
 
     void OnTriggerEnter(Collider col) {
 
         if(col.tag == "Enemy") {
             
                 col.gameObject.GetComponent<HealthEnemy>().TakeDamage(damageVal);
 
             if (!buffed && Random.value <= 0.2)
             {
                 col.gameObject.GetComponent<HealthEnemy>().TakeDamage(critVal);
                 print("Regcrit");
                 critFill.Crit();
             }
             if (buffed && Random.value <= 0.9)
             {
                 col.gameObject.GetComponent<HealthEnemy>().TakeDamage(critVal);
                 print("Bufcrit");
                 critFill.Crit();
             }
         }
     }
     public IEnumerator Buff2()
     {
         print("IamBuffed");
         buffed = true;
         yield return new WaitForSeconds(40f);
         buffed = false;
         yield return null;   
     } 
 }

 



Comment
Add comment · Show 3
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 hoekkii · Jan 08, 2017 at 03:31 PM 1
Share

You don't need to call StartCoroutine from AttackTrigger. The method comes from $$anonymous$$onoBehaviour that you inherrit. Also it is not a good practice to run something using strings. So try replacing Buff.StartCoroutine("Buff2"); with StartCoroutine(Buff.Buff2())

avatar image hoekkii · Jan 08, 2017 at 03:33 PM 0
Share

Also for future problems like this, try debugging with breakpoints or using Debug.Log() to see where it does not get called, so you know which line of code should be altered.

avatar image Omerl_ahire hoekkii · Jan 08, 2017 at 10:29 PM 0
Share

Okay got it working now, thanks for the advice.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine not working as expected 1 Answer

Randomized array result from other scipt 2 Answers

Persistent Timers in Couroutines 0 Answers

Issues with video player coroutine 2 Answers

Co-routine a recursion help 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