- Home /
Can a function maintain its argumanet data after it is run?
Thats a question thats crossed my mind when trying to figure out this problem..
I'm trying to figure out a very wierd bug. I run the following code and the first time the function is run everything is okay (a person gets loaded), but the second time it gets called it doesnt work.
randomMysteryGeneratorScript.personScript[0]=randomMysteryGeneratorScript.personObject[0].GetComponent(Person); yield Load_Person(randomMysteryGeneratorScript.personScript[0]);
//FOR SOME REASON THIS next one DOESNT LOAD THE 2ND PERSON...even though its the sme function!!!!!!!!!!!!!!!!!!!!!!!!!!!!!???????????????????????????
randomMysteryGeneratorScript.personScript[1]=randomMysteryGeneratorScript.personObject[1].GetComponent(Person);
yield Load_Person(randomMysteryGeneratorScript.personScript[1]);
What can be possibly different about the second run? There is no error as such, instead the person does not get populated with any data. (but this is fine on the first run??)
what happens if you comment out the first yield Load_Person(...);
line? does the second one execute properly in that case?
Good question, I just tried removing the first one but the second one still doesnt excecute.
Can you give some more context? Any time you use a yield, it's helpful to know what the function you're yielding does, as it's likely that's where the problem is.
Also, to answer the title of your question, a function never "remembers" arguments. See this Wikipedia article for a primer on scope: http://en.wikipedia.org/wiki/Scope_(program$$anonymous$$g)
Answer by Grimmy · Dec 21, 2010 at 02:38 PM
I just tried the following and it still doesnt work DESPITE the fact that both print commands DO display the correct names.
currentPersonScript=randomMysteryGeneratorScript.personObject[0].GetComponent(Person); print("ScriptName0:"+currentPersonScript.name); yield Load_Person(currentPersonScript);
//FOR SOME REASON THIS DOESNT LOAD THE 2ND PERSON.
currentPersonScript=randomMysteryGeneratorScript.personObject[1].GetComponent(Person);
print("ScriptName1:"+currentPersonScript.name);
yield Load_Person(currentPersonScript);
It leads me to think you have a bug in function Load_Person.
Answer by Mickydtron · Dec 21, 2010 at 05:08 PM
Well, if the second call does not work even when we do not execute the first call (thus making the "second" call the first one to actually go), then the first call is obviously not messing things up for the second one, and there is a problem with the line
yield Load_Person(randomMysteryGeneratorScript.personScript[1]);
Also, as stated above by burnumd (upvoted), the principles of scope dictate that the variables of one call will not bleed over into the next call. Unless they are static variables, which are shared across all instances of something. I was curious if the yield was not working properly, and would suggest fully expanding the yield statement. That is:
yield StartCoroutine(Load_Person(...));