Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zeronev · Oct 06, 2015 at 02:33 AM · c#errorinstantiatearraylist

Can´t instantiate objects in list correctly

The method is called when my document gameobject is picked and added to, the list i have (documents) When I have only 1 item in my list, the I can grab the documents info (title, etc), and make them show them the info in my UI when clicked, not necessary to post that method.

BUT, when I have 2 or more items in my list, I cannot access any item in the document list anymore, I believe its the int (documentIDToinstance), what this does is instantiating the correct ".document" info to the new UI element, so it shows like the actual document

  public void PickedDocumentUp()
     {
 
 
         GameObject newbuttonToInst = Instantiate(FilePrefab);
 
         newbuttonToInst.name = documents[documentIDToInstance].documentTitle;
         newbuttonToInst.transform.parent = gameObject.transform;                    //Sets where to instantiate the newButton
         newbuttonToInst.transform.localScale = new Vector3(1f, 1f, 1f);             //Sets the correct scale of the newButton
         newbuttonToInst.GetComponent<Button>().onClick.AddListener(() => { ReadClickedDocument(); }); //Set the function to read the document
 
         Component[] components;
         components = GetComponentsInChildren<Text>();
 
         components[0].GetComponent<Text>().text = documents[documentIDToInstance].documentTPreview;   //Take and set the taken document title
         components[1].GetComponent<Text>().text = documents[documentIDToInstance].documentTitle;     //Take and set the taken document body
         components[2].GetComponent<Text>().text = documents[documentIDToInstance].documentBody; //Set the document preview
         
         
         //_documentTPreview.text = documents[documentIDToInstance].documentTPreview;
 
         documentIDToInstance = documents.Count;
 
 
     }


I hope I explained myself well. Hope anybody can guide me or show me some solutions. I believe the error is when instantiating the prefabs. Thanks!

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 Inok · Oct 06, 2015 at 11:23 PM

You know that first index in array start at 0 (zero), so i not undestand why you assign here "documentIDToInstance = documents.Count;" Maybe you need "documentIDToInstance = documents.Count - 1;" Sorry if i wrong.

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 Zeronev · Oct 08, 2015 at 01:09 AM 0
Share

The error was in here:

  Component[] components;
              components = GetComponentsInChildren<Text>();

The fix was only adding this:

 Component[] components;
              components = newButtonToInst.GetComponentsInChildren<Text>();

The documents.Count will be default Zero. so it will start counting by zero :) If there's nothing in the documents List, it will be zero. But thanks for your answer! $$anonymous$$aybe it'll be useful for someone :)

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

31 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

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Cannot convert float to int when only using floats (C#) 3 Answers

How to put gameObjects to the list? 4 Answers

Problem with List not filling using add method in a foreach loop 1 Answer

Instantiate and arrays 2 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