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 El__Nacho · Feb 10, 2017 at 10:05 PM · c#coroutinereferencepointer

Reference a variable of another script

I try to setup a coroutine which can change values by time, for example change an alpha value form 0 to 1 in 5 seconds. I thought I can programm it as an extension method and call it from any script, but I don't know how to tell the coroutine which value it should change. I heard of pointers and that you can't use them in Unity.

 IEnumerator ChangeVariable(float duration,int targetVariable)
 {
     float time=0;
     while (time<duration)
     {
         time+=Time.deltaTime;
         targetVariable=time/duration;
         yield return new WaitForEndOfFrame();
     }
     yield return null;
 }

targetVariable should be only the adress of the variable this coroutine should change.

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

2 Replies

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

Answer by jdean300 · Feb 11, 2017 at 01:06 AM

You could either pass an enum into the Coroutine that you then use in a switch statement to determine what variable to set. Or, pass the Coroutine a lambda that sets the proper variable:

 private IEnumerator ChangeVariable(float duration, Action<float> op){
     float time = 0;
     while (time < duration) {
         time += Time.deltaTime;
         op(time / duration);
         yield return new WaitForEndOfFrame();
     }
     yield return null;
 }
 
 // use example, given you have this Color object 
 Color color = /* whatever */;
 StartCoroutine(ChangeVariable(duration, (val) => color.a = val));
 StartCoroutine(ChangeVariable(duration, (val) => color.r = val));

To change different or multiple values, all that needs to change is the lambda

Comment
Add comment · Show 3 · 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 El__Nacho · Feb 11, 2017 at 01:54 PM 0
Share

I think that is what I have looked for, but I when I try to use it this error occurs: The type or namespace name 'Action<>' could not be found... What do I have to do to use it?

avatar image jdean300 · Feb 11, 2017 at 03:17 PM 0
Share

You probably missed the part. The type inside the brackets indicates the types of the functions parameters.

avatar image El__Nacho · Feb 11, 2017 at 03:43 PM 0
Share

I had to use System.Action ins$$anonymous$$d of Action. Now it works and it is exactly what I wanted. Thank you :D

avatar image
0

Answer by karadag · Feb 10, 2017 at 10:31 PM

I did not fully understand. But you can call any this function.

.getComponent.StartCoroutine("ChangeVariable",3); //call function with your duration .getComponent.targetVariable //get target Variable

Example Test.cs Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test : MonoBehaviour 
 {
 
     public float targetVariable;
     public float duration = 3;
 
     void Start()
     {
         StartCoroutine ("ChangeVariable",duration);
     }
 
     void Update()
     {
         Debug.Log (targetVariable);
     }
 
     public IEnumerator ChangeVariable(float duration)
     {
         float time=0;
         while (time<duration)
         {
             time+=Time.deltaTime;
             targetVariable=time/duration;
             yield return new WaitForEndOfFrame();
         }
         yield return null;
     }
 }
Comment
Add comment · Show 1 · 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 El__Nacho · Feb 10, 2017 at 11:45 PM 0
Share

This is what I use now but I want to change for example an alpha value from inside the coroutine. In this case I had to create a coroutine for each different value( in case of a color one for r, one for b, one for g, and one for alpha). Besides with this solution only one value could be changed simultaneously.

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

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

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers

Coroutine Not Starting - Crashes Instead? 1 Answer

Moving code from external c# project library into Unity without loosing references 0 Answers

Advice on debugging a blocked main thread only on iOS 0 Answers

My coroutine won't work 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