Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cariaga · Sep 09, 2013 at 02:47 PM · javascriptarraysdatabaseinventoryinventory system

Is this considered bad coding

 db = new MonoSQLiteDB( "Model_D_Interaction.sqlite" );//name of database to access
 //db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory", true );
 db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Id", true );
 
 
 
 if((db.Result[0]["Item_Name"])){
  
     var other = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other.name =db.Result[0]["Item_Name"];
     other.transform.parent = transform;//attach the data  
     var script = other.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script.stack = parseInt(db.Result[0]["Item_Stack"]);
     AddItem(other);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 
 if((db.Result[1]["Item_Name"])){
  
     var other1 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other1.name =db.Result[1]["Item_Name"];
     other1.transform.parent = transform;//attach the data  
     var script1 = other1.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script1.stack = parseInt(db.Result[1]["Item_Stack"]);
     AddItem(other1);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 if((db.Result[2]["Item_Name"])){
  
     var other2 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other2.name =db.Result[2]["Item_Name"];
     other2.transform.parent = transform;//attach the data  
     var script2 = other2.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script2.stack = parseInt(db.Result[2]["Item_Stack"]);
     AddItem(other2);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 
 
 if((db.Result[3]["Item_Name"])){
    
     var other3 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other3.name =db.Result[3]["Item_Name"];
     other3.transform.parent = transform;//attach the data  
     var script3 = other3.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script3.stack = parseInt(db.Result[3]["Item_Stack"]);
     AddItem(other3);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 if((db.Result[4]["Item_Name"])){
  
     var other4 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other4.name =db.Result[4]["Item_Name"];
     other4.transform.parent = transform;//attach the data  
     var script4 = other4.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script4.stack = parseInt(db.Result[4]["Item_Stack"]);
     AddItem(other4);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 if((db.Result[5]["Item_Name"])){
  
     var other5 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other5.name =db.Result[5]["Item_Name"];
     other5.transform.parent = transform;//attach the data  
     var script5 = other5.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script5.stack = parseInt(db.Result[5]["Item_Stack"]);
     AddItem(other5);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 
 
 if((db.Result[6]["Item_Name"])){
  
     var other6 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
     other6.name =db.Result[6]["Item_Name"];
     other6.transform.parent = transform;//attach the data  
     var script6 = other6.GetComponent(Item);  
     db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
      script6.stack = parseInt(db.Result[6]["Item_Stack"]);
     AddItem(other6);
 yield WaitForSeconds(2);
     
 //print((db.Result[0]["Item_Name"]))
 
 }
 
Comment
Add comment · Show 2
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 cariaga · Sep 09, 2013 at 03:44 PM 0
Share

I FIGURED IT OUT if anybody needs the code here you go


 for (var i=0;i<db.Result.Length;i++) {
 var x = 0;
 
     if((db.Result[x]["Item_Name"])) {
         var other = Instantiate(ContentCollections[x], transform.position, Quaternion.identity);
         other.name =db.Result[i]["Item_Name"];
         other.transform.parent = transform;//attach the data  
         var script = other.GetComponent(Item);  
         db.Result = db.ExecuteFreeQuery( "SELECT * FRO$$anonymous$$ Player_inventory WHERE Item_Stack", true );
          script.stack = parseInt(db.Result[i]["Item_Stack"]);
         AddItem(other);
       // yield WaitForSeconds(2);
  
        //print((db.Result[i]["Item_Name"]))
  x++;
     }
 }
avatar image Fattie · Sep 09, 2013 at 03:46 PM 0
Share

"Any time you repeat ANYTHING, it is bad coding..."

it's one way to look at things.

Another absolutely critical point .. is it true that you only WANT TO DO ONE OF THESE (the first one that "comes up"), or did you actually mean to do them all.

1 Reply

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

Answer by mattssonon · Sep 09, 2013 at 03:02 PM

Yes, it is. You should wrap it in, for instance, a loop:

 for (int i = 0; i < Result.Length; i++) {
     if((db.Result[i]["Item_Name"])) {
         var other = Instantiate(ContentCollections[i], transform.position, Quaternion.identity);
         other.name =db.Result[i]["Item_Name"];
         other.transform.parent = transform;//attach the data  
         var script = other.GetComponent(Item);  
         db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
          script.stack = parseInt(db.Result[i]["Item_Stack"]);
         AddItem(other);
         yield WaitForSeconds(2);
      
         //print((db.Result[i]["Item_Name"]))
      
     }
 }
Comment
Add comment · Show 2 · 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 cariaga · Sep 09, 2013 at 03:31 PM 0
Share

Instantiate only occurred ones, And the result was IndexOutOfRangeException when i< reached 2 i was thinking a for loop will do before but still end up with IndexOutOfRangeException.

avatar image cariaga · Sep 09, 2013 at 03:42 PM 0
Share

O$$anonymous$$ i figured it out THAN$$anonymous$$S for THE note about my "Code smell"

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need some help on inventory. 0 Answers

Adding Muiltipule GUI.Buttons In an Inventory System Based On Arrays 1 Answer

How to optimize this script and add items imediately without grids 0 Answers

Inventory Drop Function Problem 1 Answer

Array index is out of range? 1 Answer


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