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 Napalm6b · Dec 01, 2013 at 09:22 PM · javascriptinputarrayslooping

I'm having trouble taking a game object from an array and storing it in a variable

So I've setup an array for a very basic inventory for three items. The idea is if a player hits 1,2 or 3 on the keyboard then the program loops through the array, finds the object and should store it in the variable. It's not working I'm having a hell of a time figuring this out.

Here is my code:

 var inventory = new Array(); 
 var index : int = 0;
 var equipped : GameObject;
 
 //this is a bit more complicated, but else if is an example of the structure for the different items
 function OnTriggerEnter(collider : Collider) 
 {
     else if(interaction.tag == "item" && interaction.name == "scroll1")
     {    
         inventory[index] = interaction.gameObject;
         index++; 
         interaction.gameObject.collider.enabled = false;
         interaction.gameObject.GetComponent(MeshRenderer).enabled = false;
     }
 }
 //records button pressed and passes variable to the CheckInventory function
 function Controls()
 {
     var keyboard : boolean;
     if(Input.GetButtonDown("item1"))
     {
     keyboard = Input.GetButtonDown("item1");
     CheckInventory(keyboard);    
     }
     else if(Input.GetButtonDown("item2"))
     {
         keyboard = Input.GetButtonDown("item2");
         CheckInventory(keyboard);    
     }
     else if(Input.GetButtonDown("item3"))
     {
         keyboard = Input.GetButtonDown("item3");
         CheckInventory(keyboard);    
     }
 
 }
 //this function is supposed to store an item from inventory in the equipped variable, it's storing the variable, although the Debug.Log shows up in the console when I hit a key
 function CheckInventory(button : boolean)
 {
     var item : GameObject;
     for(item in inventory)
     {
         if(item.name == "wand1" && button == Input.GetButtonDown("item1"))
         {
             equipped = item;
             Debug.Log("fire");
         }
         else if(item.name == "scroll1" && button == Input.GetButtonDown("item2"))
         {
             equipped = item;
             Debug.Log("green");
         }    
         else if(item.name == "scroll2" && button == Input.GetButtonDown("item3"))
         {
             equipped = item;
             Debug.Log("electric");
             
         }
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by iwaldrop · Dec 01, 2013 at 10:11 PM

This code is way more complicated than it needs to be, and I'm surprised it even compiles. I guess unityscript is more relaxed, but it shouldn't be.

In your CheckInventory method you declare item to be empty before your loop. Take that line out. Also, why are you passing a boolean to it? You don't use it. The entire method is pointless, and effectively doubles the work that you're already doing in your Controls method. Why don't you just set an index number based off of which button is pressed that will reference the given item when needed? Why are you checking for a button input 3 times before doing anything with it? There is no benefit, and it only slows things down.

 var activeIndex : int = 0;

 function Controls()
 {
     if(Input.GetButtonDown("item1"))
         activeIndex = 0;
     else if(Input.GetButtonDown("item2"))
         activeIndex = 1;
     else if(Input.GetButtonDown("item3"))
         activeIndex = 2;
 }
 
 function GetActiveInventoryObject() : GameObject
 {
     return inventory[activeIndex];
 }
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 Napalm6b · Dec 01, 2013 at 10:34 PM 0
Share

well good sir I'm just learning at this point thanks for the program$$anonymous$$g lesson!

I thought a bit about this and realized my code was a tangled mess, and with that you didn't understand what it did.

First the booleans were used; wacky old javascript allowed me to set the data types of the variables to anything I want to, so the Input buttondown was saved in the keyboard variable and passed to the 2nd function to check which key was pressed.

Second I didn't know what order items would go into the array so I didn't think indexes alone would work, I decided to iterate over the array of GameObjects with a GameObject type variable and pull out the one that matched the name I wanted to store in equipped based on button variable.

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

17 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

Related Questions

Add an object to Array 2 Answers

Instantiating prefabs to every object in an array? 1 Answer

how to change default third player controls in unity, to work with touch? 1 Answer

Making a character rotate diagonal? 1 Answer

Eliminating input loss 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