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 Ephesiel · Apr 26, 2018 at 07:07 AM · gameobjectlistupdate

How is it possible that my second list always has the same length that the first one even when it changes ?

Here is my code

 private SelectionItems SI;
 
 private List<GameObject> _selectedList;
 private List<GameObject> _lastSelectedList;
 
 private bool _needChange;
 
 void Start () {
     
     _needChange = true;
     SI = GetComponent<SelectionItems>();
 }
     
 void Update () {
 
    // Get selected objects via a list
     _selectedList = SI.GetSelectedList();
 
     // Check if both lists have the same length
     if (_selectedList.Count == _lastSelectedList.Count)     { _needChange = false; }
     else                                                    { _needChange = true; }
 
     // Change the second list AFTER the if
     _lastSelectedList = _selectedList;
 }


I want to understand why, _needChange is always equal to false, even when i change my selection in game. I mean, indeed the length changes depending on my selection, but both lists always have the same length. When _selectedList.Count changes, _lastSelectedList.Count also changes BUT BEFORE THE IF and I don't know why

Thanks for help :D

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 Anne-Pier · Apr 26, 2018 at 08:58 AM 0
Share

Use Debug.Log and see if needChange is ever true, it should be for just 1 frame though, you set lastselectedList.Count equal to selectedList.Count at the end of every frame, so even when you change your selection, it lasts 1 frame only. Please try a debug.log and see if it actually ever is true.

avatar image Ephesiel Anne-Pier · Apr 26, 2018 at 09:01 AM 0
Share

Yes i tried it. needChange is always true. I tried with a return; in the else and the method never return

avatar image Anne-Pier Ephesiel · Apr 26, 2018 at 09:06 AM 0
Share

needChange is always true? But you said it was always equal to false lol, why don't you try it with an InvokeRepeating on something other than the update, something that only happens every 5 seconds so you can clearl ysee what happens... It's hard to see when Update literally happens every frame so you can't see what happens in your code.

 void Start()
 {
     InvokeRepeating("$$anonymous$$ethodName", 1, 5);
     _needChange = true
     SI = GetComponent<SelectionItems>();
 }
 
 void $$anonymous$$ethodName
 {
     // Code you have in the update + Debug.Log(_needChange) and make the variable public so you can see it in the inspector and see if it changes.
 } 
Show more comments
avatar image Harinezumi · Apr 26, 2018 at 11:18 AM 0
Share

How do you initialize _lastSelectedList? You would have to initialize it before Update() is called the first time, or the if() statement will compare int to null (which throws a NullReferenceException).
Also, what does SelectionItems.GetSelectedList() return? Does it do a return someInternalGameObjectList;? Because then _selectedList will refer to the list inside that SelectionItemsinstance, so it will actually never change... and _lastSelectedList will also just "point" to the same object instance, so whatever happens with it, will also be reflected in these variables (which explains why the if() is always true). You would have to return a copy of a list in SelectionItems to actually see a difference.

1 Reply

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

Answer by Anne-Pier · Apr 26, 2018 at 11:03 AM

Figured it out, should've known this sooner. Fix it by using

 _lastSelected.AddRange(_selectedList);

Reason this happens is because you make last lastselected equal to selectedlist, this is because you should see it as a variable. If you put the Transform of an object inside a variable, it will change along with the Transform, this is what happened.

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 Ephesiel · Apr 26, 2018 at 11:15 AM 0
Share

Nice it works, I just need to add _lastSelectedList = new List<GameObject>(); before, and it works perfectly ! Thanks a million

avatar image Anne-Pier Ephesiel · Apr 26, 2018 at 11:15 AM 0
Share

No problem :) should've definitely gotten to this sooner though but glad it works now finally haha.

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

124 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

Related Questions

A node in a childnode? 1 Answer

Add GameObject to List using Timer 1 Answer

Update list on mouse click 1 Answer

Adding a prefab to gameObject at a certain position in runtime 0 Answers

Efficiency of Game Loops 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