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.
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
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?
You probably missed the part. The type inside the brackets indicates the types of the functions parameters.
I had to use System.Action ins$$anonymous$$d of Action. Now it works and it is exactly what I wanted. Thank you :D
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;
}
}
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
![](https://koobas.hobune.stream/wayback/20220612110819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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