- Home /
Question about C# and using Methods...
Hi, I am currently trying to create a game that runs a basic thermostat
I have a basic function that that when a GUI button is clicked, it checks the if statement, if thats true, then decreases the ambient temperature by 1 every three seconds
Here is the code of the function...
int Choice;
IEnumerator AC()
{
int DesiredInt = 65;
int AmbientInt = 78;
int Choice = 0;
if (Choice == 0)
{
if (AmbientInt > DesiredInt)
{
yield return new WaitForSeconds(3);
AmbientInt -= 1;
}
}
}
Here is the code I am trying to use to when I click the GUI button, it runs this function
if (GUI.Button(ACRectPercent,"A/C On"))
{
AC()
{ // <--- LINE 75 HERE!
}
}
I keep getting an error for the top brace under "AC" that says
"Assets/Scripts/ACon.cs(75,25): error CS1525: Unexpected symbol `{'"
Ive never written anything in C# for Unity, so its been kind of a learning process. Any help as to why I am getting this error would be greatly appreciated, or any pointer on how to screw up less would also be appreciated
Please let me know if you need any extra info!
Answer by paulaceccon · Mar 16, 2013 at 10:03 AM
To call a co-routine, you have to use StartCoroutine(AC());
You just cleared up the last of my issues! Thank you very much!!!!
Answer by Khada · Mar 16, 2013 at 06:32 AM
In short it should be 'AC();'. If you give it braces, it thinks you are trying to define a method (or redefine in this case). Also, I don't think you can yield within the OnGUI method but I'm not sure.
Well I got the error to go away, but I get no activity when I click the button now =( Thxs though for your help!
I'm glad the error's gone. Like I said, I don't think yielding from the OnGUI method will give you any results or the results you want/expect. Better to just use a few floats with Time.deltaTime in the Update method.
Also, can you please mark the question as answered (gray tick icon)? It helps keep the site clean and gives credit to the volunteers who help :)
Well its not even the yield, I threw a debug.log at the very beginning of the function just to see if it was even reading it and got nothing, would the yield stop the function from even being called?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Instantiating Prefabs through Editor Script 1 Answer
Problem with getting a value from a enum 2 Answers
Best way to limit the functionality of Lua scripting? 1 Answer
Distribute terrain in zones 3 Answers