If I call a function within a coroutine, will the called function be running within the coroutine?
No.
This is good to know and explains allot of my woes. I’m trying to pass arguments to a IEnumerator, and it works, but it always throws null exceptions when doing so, despite it working.
Well, describe what you’re trying to do and post your code… maybe someone can offer a suggestion/fix or something.
It can be something very simple, assume have a function as such:
IEnumerator PrintSomething(string str) {
print(str);
}
Now on my start I might call:
StartCoroutine(PrintSomething("some string"));
This is obviously different than the prescribed method of starting a coroutine, but it “works”. Still throws a meaningless null exception though.
There’s no such thing as a meaningless null exception. Things can very well become invalid during a coroutine. Something outside the coroutine must be invalidating your object, either by destroying a script or changing the data, or you’re accessing data that might not be ready within the coroutine. It’s hard to say since you haven’t given us a real example from your project.
private Rigidbody rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
StartCoroutine(Faster(1.0f));
}
// runs perfectly fine...
IEnumerator Faster(float startingSpeed)
{
float speed = startingSpeed;
while(true)
{
rigidbody.velocity = new Vector3(speed, 0f, 0f);
speed += 10.0f * Time.deltaTime; // go faster!
yield return null;
}
}
// ... until we collide with something, then the coroutine will start throwing errors
void OnCollisionEnter(Collision collision)
{
// we've hit something, destroy the rigidbody to keep us in place
Destroy(rigidbody);
}
There’s absolutely nothing wrong with passing parameters to a coroutine.
Indeed… I believe you will have to show the actual source (code) - something is not right
Point out what’s giving you the null ref, also, as that’s helpful
The code you posted won’t compile (as PrintSomething doesn’t yield or return anything), but if you do it correctly things will work as expected:
using System.Collections;
using UnityEngine;
public class Foo : MonoBehaviour
{
public float timeToWait = 1f;
public string stringToPrint = "hello";
private void Start()
{
StartCoroutine(WaitThenPrint(timeToWait, stringToPrint));
}
private IEnumerator WaitThenPrint(float seconds, string str)
{
yield return new WaitForSeconds(seconds);
Debug.Log(str);
}
}
I don’t understand the question. What do you mean with “within the coroutine”?
Calling a function from within a coroutine isn’t different than calling a function anywhere else.
The example I gave above gives me the null ref as well. It might be my installation, I will re-install when I get back home. Thanks for the help.
I see. Ok.
What is print
?
I’d say the problem has to lie in there. If you are calling start coroutine, and you have
IEnumerator PrintSomething(string msg) {
//Print logic
yield return null;
}
Then your code will work. Edit: This is wrong Except, we haven’t seen what this “print” function is. That is the only unknown here, and it almost has to be the source of the issue.
print is actually a static method on monobehaviour (I believe) that maps to Debug.Log.
Print is the same as Debug.Log as far as I know.
Ah, good to know. Thanks!
Hey thanks for the reply, I just typed up a generic function while at work. I forgot the return. It doesn’t matter how simple it is, it will throw a null exception if I pass something. Print works fine, I was just using it as a generic foo statement for example.
At what line is the null exception?, is it on the coroutine?
I hate to sound argumentative, so please forgive me if I do, but the overwhelming majority say that your simple foo statement works.
void Start() {
StartCoroutine(GetUnityHelp(yourFunction));
}
IEnumerator GetUnityHelp(string yourFunction) {
if ((fooFunction == Works) && (yourFunction != Works)) {
yourFunction == DifferentThanFooFunction;
TryPrinting(differentFooFunction);
fooFunction = differentFooFunction;
}
yield return new WaitUntil(()=> fooFunction == yourFunction);
}
Right, I’m not arguing. I, as in my build/setup/computer/installation IS having this issue. This issue does exist for me, personally. As such, I’m now trying to debug it, since I know I’m not doing something wrong.
Edit: I just blasted out everything, re-installed, and everything seems to be working properly now. I’m not getting that null exception from simple coroutines.
I appreciate everyone’s replies.
Glad it’s working for you now