- Home /
Can't translate bool from C# to unityscript
I am trying to convert this part of a script from C# to unityscript.
PART OF IT:
public bool CanAttack { get { return shootCooldown <= 0f; } }
I don't know much of C#, I was used of seeing ; in the end, or of function which started with public void ... But this has none of it. And I am not sure how to do it, is this the right way? :
public var CanAttack : boolean; { get { return shootCooldown <= 0f; }
}
It works now. SOLUTION:
public function get CanAttack() : boolean
{
return shootCooldown <= 0f;
}
Answer by FairGamesProductions · Nov 06, 2014 at 10:21 PM
This may be a little un-orthodox, but the simplest way I found to transfer variables from any script to any other script, without depending on compile order or any of that mess, are empty GameObjects (for bool variables). For example (JS):
private var MyBooleEmptyGameObject : GameObject;
private var BoolInThisScript = false;
function Start ()
{
MyBooleEmptyGameObject = GameObject.Find ("/Variables/MyBooleEmptyGameObject");
if (MyBooleEmptyGameObject.transform.localPosition.y > 0.5)
{
BoolInThisScript = true;
}
else
{
BoolInThisScript = false;
}
}
I usually keep all my Empty GameObject variables under another Empty GameObject called "Variables", to keep things neat. I also use the 0.5 Y position as the threshold between true and false, as sometimes in Unity GameObjects MAY change their position by 0.000000001 So using > 0.5 or < 0.5 is the safe bet.
The same process can be done with string, int or float.
Your answer
Follow this Question
Related Questions
Mecanim Boolean 1 Answer
Shields 'always' inactive. Player object doesn't recognise if child exists(Solved) 1 Answer
Detect perimetral walls of a room 1 Answer
Spawn Object If Boolean Equals True, Boolean Set To True On Level Load 1 Answer
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer