Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Goatcheesecough · Jul 15, 2015 at 10:54 AM · scripting problem

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;
 
 
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 15, 2015 at 02:40 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goatcheesecough · Jul 16, 2015 at 08:04 PM 0
Share

All these answers were good but this one was easiest to understand for me. Thank you.

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[JS] Match 3 game - How to compare certain values stocked in a multi-dimensional Array 1 Answer

Adding constraint component at runtime 1 Answer

help with animation 1 Answer

Why my editor vs code opens a few windows when i try to open a script from unity,My VS Code open a few tables when i double click a script in Unity? 0 Answers

[Unity5] Performance and functions 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges