Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Animaki · Apr 30, 2020 at 09:07 PM · listarraylist

How do I make the objects on my list activate according to the variable?

I would like to ask a question, I am making a game of ships, I was creating a list with the objects (slots) that would be disabled initially, however they would be activated according to the number of slots stored in the variable, I put it in the script as public and added it in the inspector the objects on the scene in their proper places in the list, but this error appears: "Index was outside the bounds of the array". What would I need to do for this to work?

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ItensId : MonoBehaviour {

 public GameObject[] Slot;
 public List<Text> IdText;
 private bool InventoryFull;
    
 void Start()
 {
     InventoryFull = false;
     DataBase.CountItens = PlayerPrefs.GetInt("CountItens");
 }

 // Update is called once per frame
 void Update()
 {
     if(ItensContent.buy == true && InventoryFull == false)
     {
         Slot[DataBase.CountItens].SetActive(true);
         ItensContent.buy = false;
     }        
 }

} obs: The calculation of the items is done in a variable from another script.

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

Answer by cam74 · Apr 30, 2020 at 10:47 PM

@Animaki An array starts from 0 ang goes to n-1 with n being the number of thing in your array. You should add - 1 to your 16th line after database.countitems. If you have 3 items the 3rd item will be slot[2] Because: Slot[0] Slot[1] Slot[2]->the third object


Vote up if I answered you

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 Animaki · Apr 30, 2020 at 11:31 PM 0
Share

Unfortunately not, this is the script that calculates CountItens, it works from a button to select the item (S$$anonymous$$S) and the other to buy the item (Buy). This and the other script are in different scenes. If this information helps you to identify the problem, I am grateful.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ItensContent : $$anonymous$$onoBehaviour { static public string id; private string ChosenItem; static public bool buy;

 void Start()
 {
     buy = false;
 }

 public void Purchase()
 {
     DataBase.CountItens = DataBase.CountItens + 1;
     PlayerPrefs.SetInt("CountItens", DataBase.CountItens);
     id = ChosenItem;
     buy = true;
 }
 public void Sms()
 {
     ChosenItem = "sms";
 }
 public void Home()
 {
     UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene("Home");
 }

}

obs: The CountItens variable is in a static script serving only as a stored database.

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

129 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Combine Children Dictionary in place of Hashtable? 2 Answers

How to find IndexOf vector3 List ? position search 2 Answers

Arraylist vs List,Arraylist vs list 2 Answers

What do you think of this? List, Vector 3 and Update 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