- Home /
Nested Functions
Firstly, heres my code:
var target : StartSprite;
var targetScript : ChangeSprite;
var AcceptInput : boolean = true;
private static var score : int = 0;
var guiScore : GUIText;
function Awake () {
function OnMouseDown () {
if(!AcceptInput)
{
return;
}
AcceptInput = false;
if(target.spriteRenderer.sprite == target.diamond) {
score +=1;
guiScore.text = "Score: " + score;
yield WaitForSeconds (0.5);
targetScript.enabled = true;
}
else {
yield WaitForSeconds (0.5);
targetScript.enabled = true;
Debug.Log("Wrong Answer");
}
}
function OnMouseUp () {
AcceptInput = true;
targetscript.enabled = false;
changeSprite ();
}
}
}
function changeSprite (){
function OnMouseDown () {
if(!AcceptInput)
{
return;
}
AcceptInput = false;
if(target.spriteRenderer.sprite == target.diamond) {
score +=1;
guiScore.text = "Score: " + score;
yield WaitForSeconds (0.5);
targetScript.enabled = true;
}
else {
yield WaitForSeconds (0.5);
targetScript.enabled = true;
Debug.Log("Wrong Answer");
}
function OnMouseUp () {
AcceptInput = true;
targetscript.enabled = false;
changeSprite ();
}
}
}
Theres a compiling error with nested functions. I searched this up on the internet. I heard that you can't use nested functions in unityscript. I'm a newbie to this. Does anyone have anyway to make the same effect? I really don't know what to do. What's an alternative to nested functions?
functions should not be defined within other functions. what are you trying to achieve?
Get the same effect with unity script. I'm asking what is the alternative with nested functions @gjf
the formatting is less than ideal, but the code that you've posted looks like you have pasted the same On$$anonymous$$ouseDown()
and On$$anonymous$$ouseUp()
code into Awake()
and changeSprite()
and i'm struggling to understand why.
unity will automatically call some functions if you've defined them, such as On$$anonymous$$ouseDown()
, On$$anonymous$$ouseUp()
, Awake()
, etc.
what are you trying to do that makes you think that nested functions are the solution?
Answer by MrSteve1 · Aug 14, 2014 at 12:38 PM
Yeah, function cannot be called inside other function, so you will either need to take them out or for example where your call
function OnMouseUp()
{
blah blah blah
}
will need to ba changed to
if(Input.GetMouseButtonUp((0)
{
blah blah blah
}
Hope this helps!
Yeah!! I never thought of that! One problem, I tried to add Input.GetTouch(0)) but it doesn't work. It gives an error saying something can't be used as a boolean
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Function being called when statement untrue? 1 Answer
Script error help! 1 Answer
Robots Won't Spawn With Script 2 Answers