Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jaidan · Feb 09, 2015 at 08:57 PM · c#coroutinepublicvoid

Start a Coroutine in script A, from script B C#

I have my first script, called "IfLoose", A and my second script called "CollisionPlayer", B. I have a public void in sciprt A and i need to start it from script B. I have it so in script A, void includes stuff for having a animation play and stoping the time and i would like it to start when a collision occurs, in script B.

Here is script A:

using UnityEngine; using System.Collections;

public class IfLoose : MonoBehaviour {

 bool AudioListener.pause;
 public GameObject looseMenuPanel;
 private Animator anim;
 
 void Start () {

     Time.timeScale = 1;
     anim = looseMenuPanel.GetComponent<Animator>();
     anim.enabled = false;
 }
 
 public void LooseGame(){

     anim.enabled = true;
     anim.Play("LooseMenu");
     Time.timeScale = 0;
     AudioListener.pause = true;
     
 }
 

}

Script B:

using UnityEngine; using System.Collections;

public class CollisionPlayer : MonoBehaviour {

 public void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Environment")
         StartCoroutine ("LooseGame");
 }

}

Thank you :D

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Deathdefy · Feb 09, 2015 at 09:18 PM

Didn't test it but this should do the trick as long as you properly find the GameObject that you need.

 public IEnumerator LoseGame()
  {
      //Add what you need here
      return null;
  }
         
  public void OnCollisionEnter(Collision col)
  {
       if (col.gameObject.tag == "Environment")
       {
           //Find the gameobject with lfLoose attached to it
           GameObject obj = GameObject.Find("ObjectToFind");
           StartCoroutine(obj.GetComponent<lfLoose>().LoseGame());
       }
  }
       

   


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 Owen-Reynolds · Feb 09, 2015 at 09:33 PM 1
Share

Or, to rephrase, the name in quotes is a shortcut, which won't work for fancy stuff like coroutines in other scripts. The above is the "real" way to call a coroutine.

Then, to find a coroutine, its just the same "find something in another script" rules as anything else. Finding alice's coroutine is the same as finding alice's int hit points;.

avatar image Jaidan · Feb 10, 2015 at 04:20 PM 0
Share

So what Deathdefy put is right? or wrong? and what srcipt would i put this in, A or B? Sorry for questions, im kinda a newbie :D

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

StartCoroutine in a public static void 1 Answer

Coroutine gets stuck randomly? 1 Answer

Yield not working properly 1 Answer


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