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 /
  • Help Room /
avatar image
-1
Question by MikGames · Jan 30, 2018 at 06:47 PM · gameobjecttransformchildlistsfor-loop

For Loop: Different count of objects are added to lists

Hello,

I'm trying to add the same amount of objects to two different Lists (bool and string).

The two Lists should have the same amount of index (8 in this case)

         for (int i = 0; i < 8; i++)
         {
             index = 0;
             index = Random.Range(0, childs.Count);

             SaveGame.currquestobjects.Add(true);
             SaveGame.questobjnames.Add(childs[index].name);

             Debug.Log(SaveGame.questobjnames.Count);

             transform.GetChild(index).gameObject.SetActive(false);
             childs.RemoveAt(index);
         }


After this, the object should be deactivated and removed from the "childs" list.

My problem: The bools list (currquestobjects) has a Count of 7 while the "questobjnames" has one of 8

Will be thankful for your help!!!!

Sincerely, MG

Comment
Add comment · Show 8
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 victorbisaev · Jan 30, 2018 at 07:03 PM 1
Share

How the code works if you replace "8" with "1" ?

avatar image MikGames victorbisaev · Jan 30, 2018 at 07:15 PM 0
Share

Both lists have the same count of objects

avatar image victorbisaev MikGames · Jan 30, 2018 at 08:25 PM 1
Share

Great, so if you continue try 1,2,3,...,8 then at what numbers it fails?

Show more comments
avatar image TreyH · Jan 30, 2018 at 07:05 PM 1
Share

Are you sure that there are at least 8 elements in the childs[] array?

avatar image MikGames TreyH · Jan 30, 2018 at 07:15 PM 0
Share

Yes, I've already checked this

avatar image TreyH · Jan 30, 2018 at 07:42 PM 1
Share

I attached this to a GameObject with 8+ empty children and never had a problem, is anything else manipulating your lists? Where is this being called?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ListCountWrong : $$anonymous$$onoBehaviour
 {
     private List<bool> questObjects;
     private List<string> questObjectNames;
     private List<Transform> childs;
 
     // Use this for initialization
     void Start ()
     {
         this.childs = new List<Transform> ();
         this.questObjects = new List<bool> ();
         this.questObjectNames = new List<string> ();
 
         for (int k = 0; k < this.transform.childCount; k++) {
             this.childs.Add (this.transform.GetChild (k));
         }
 
         for (int i = 0; i < 8; i++) {
             
             int index = 0;
             index = Random.Range (0, childs.Count);
 
             this.questObjects.Add (true);
             this.questObjectNames.Add (childs [index].name);
 
             Debug.LogFormat ("questObjects: {0}, questObjectNames: {1}", this.questObjects.Count, this.questObjectNames.Count);
 
             childs [index].gameObject.SetActive (false);
             childs.RemoveAt (index);
         }
     }
 }
avatar image MikGames TreyH · Jan 30, 2018 at 07:46 PM 1
Share

I've just tested and changed several things in several methods and I don't know how and why, but now it works!!!!

Thank you very much for your effort testing this and for your answer!!

0 Replies

· Add your reply
  • Sort: 

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

123 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

Related Questions

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. 1 Answer

Save gameobjects in a list before destoying 0 Answers

How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer

Assets/CArSmoothFollow.cs(40,68): error CS0246: The type or namespace name `ExampleGame_Game' could not be found. 1 Answer

Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 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