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 MauzLord · Oct 17, 2017 at 06:52 AM · scripting problemprefabvariablecoroutinereference

How to use a float value from coroutine 1 and use in coroutine 2?

Hi,

I have two scripts that are attached to different objects. Script 1 with Coroutine 1 are attached to a prefab. Script 2 and Coroutine 2 are attached to an empty spawner object.

Coroutine 1 has a variable called time. It randomly chooses a number and stores into that variable, like ex. time = 3. What I need is to use that variable in Coroutine 2 so that I can yield WaitForSecond(time).

The problem is that I`m able to set a reference to the Script 1 and reference the variable time. But the Script 2 cannot use that value. When I Debug.Log(time) in the Script 2 it says 0 all the time. However, if I debug it in the Script 1 even outside the coroutine function it shows the right number in the console.

How can I make it work in the Script 2 as well?

Script 1:

 IEnumerator Coroutine1(){
         
         time = Random.Range (1f, 3f);

Script 2:

  IEnumerator Coroutine2(){
         
             while (true) {
     
                  Rigidbody2D hazard = Instantiate (hazardBlock, hazardPosition, Quaternion.identity);
                         Debug.Log(time); // this shows  0 all the time
                 yield return new WaitForSeconds (time); // this is 0 all the time

Thank you.

Comment
Add comment · Show 1
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 MauzLord · Oct 17, 2017 at 03:00 PM 0
Share

@Eric5h5 $$anonymous$$ay I ask for your help? I know that you`re very good at coroutines.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unit_nick · Oct 17, 2017 at 08:00 AM

dunno what you're doing wrong. the following works

 public float time;
 private void Start()
 {
     time = 0;
     StartCoroutine(Coroutine1());
     StartCoroutine(Coroutine2());
 }
 IEnumerator Coroutine1()
 {
     while (true)
     {
         time = Random.Range(1f, 3f);
         yield return new WaitForSeconds(time);
     }
 }
 IEnumerator Coroutine2()
 {
     while (true)
     {
         //Rigidbody2D hazard = Instantiate(hazardBlock, hazardPosition, Quaternion.identity);
         Debug.Log(time); 
         yield return new WaitForSeconds(time);
     }
 }
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 MauzLord · Oct 17, 2017 at 02:59 PM 0
Share

You missed the part in the beginning where I said these are 2 different scripts attached to 2 different game objects (empty object and prefab).

avatar image unit_nick MauzLord · Oct 17, 2017 at 03:12 PM -1
Share

nah not really. I simply answered in context to your question. this also works (when in separate scripts) remember that i'm answering on the basis that you have said

I`m able to set a reference to the Script 1 and reference the variable time.

so I'm demonstrating that any correctly referenced variable will work

 using System.Collections;
 using UnityEngine;
 
 public class Script1 : $$anonymous$$onoBehaviour
 {
     public static float time;
 
     void Start()
     {
         time = 0;
         StartCoroutine(Coroutine1());
     }
 
     IEnumerator Coroutine1()
     {
         while (true)
         {
             time = Random.Range(1f, 3f);
             yield return new WaitForSeconds(time);
         }
     }
 }
 
 public class Script2 : $$anonymous$$onoBehaviour
 {
     void Start()
     {
         StartCoroutine(Coroutine2());
     }
 
     IEnumerator Coroutine2()
     {
         while (true)
         {
             Debug.Log(Script1.time);
             yield return new WaitForSeconds(Script1.time);
         }
     }
 }


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

143 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

Related Questions

Simplify this code 3 Answers

How do I assign a prefab reference to a private variable? 1 Answer

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes) 1 Answer

Creating a Prefab and Keeping Original Objects Script Information 0 Answers

How to access a script on a instantiated prefab 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