- Home /
Placing brackets in a script
Hello everyone. I'm working on a game right now and I tried to make a script for a shooting bow. This is the first script I've really written on my own and I need help placing brackets. If someone could show me where to put the brackets and also explain in general where to put them it would be greatly appreciated.
#pragma strict
var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Ammo : int;
var projectile : Rigidbody;
var speed = 10;
var nonex;
var snonex;
var FireRate : float = 1.0;
private var NextFire : float;
function Update ()
if (Input.GetKeyDown(KeyCode.R));
if (Ammo > 0)
SwapWeapons();
nonex = true;
function SwapWeapons()
if (Weapon01.active == true);
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(true);
//else
//
//Weapon01.SetActiveRecursively(true);
//Weapon02.SetActiveRecursively(false);
if nonex = true{
if(Input.GetButtonDown("Fire1"))
//play animation
//play sound
snonex = true
if snonex = true
if (Input.GetButtonUp("Fire1") && Time.time > NextFire)
NextFire = Time.time + FireRate;
clone = Instantiate(projectile,transform.position,transform.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0,0 speed));
NextFire = Time.time + FireRate;
Destory(clone.gameObject,5;
Just read any regular book or website about computer program$$anonymous$$g. $$anonymous$$ost progra$$anonymous$$g languages are pretty similar, and have the same rules about curly-braces, semi-colons, if statements ... .
What you have is unityScipt, which is only for Unity, so has less places to read about it (but it's very, very similar to everything else.) C# is the other Unity language, which is used for lots of other things, so has many more "how to learn C#" books.
Answer by CaptainKirby · Jul 15, 2015 at 11:13 AM
Brackets are used thusly:
function FunctionName(parameters)
{
Stuff that happens inside function are enclosed by brackets
}
the same applies for if:
if(something happens)
{
Do stuff
}
else if(something else happens)
{
Do stuff
}
else
{
Do stuff
}
All these answers were good but this one was easiest to understand for me. Thank you.
Answer by fafase · Jul 15, 2015 at 11:22 AM
Brackets define a scope. A variable exists within the brackets in which it is created:
{ // Scope A
float f = 0.0f;
{ // Scope B
float g = 0.0f;
f = 10.0f;
}
g = 10.0; // Error
}
{ // Scope C
// f and g do not exist
}
What is created in scope A also exists in scope B since Scope B is nested. What is created in scope B only exists in scope B and any nested scopes (none in this case). Scope C does not see what was created in Scope A or B and vice-versa.
Scopes are used to define methods, actions or even classes/structs. What is created in a class can be seen outside of its scope by using access modifiers (public, protected, internal).
Brackets define the limits of an action like an if/switch/while and so on:
if(condition)
{ }
else if(other){}
if the content is only one line, the brackets are not used.
if(condition) MyAction();
I tend to avoid this since it loses readability:
if(condition)
if(other)
MyAction(); // Bad according to me.
if(condition)
Action();
MyAction();
MyAction() always run though one may wrongly think it is part of the if. Particularly if there is a lot of other conditions all around and indentation is badly done.
Same applies for variables, whatever is created in the if does not exist in the else if or anywhere else.
Your answer