- Home /
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
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.
Yes i tried it. needChange is always true. I tried with a return;
in the else and the method never return
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.
}
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 SelectionItems
instance, 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.
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.
Nice it works, I just need to add _lastSelectedList = new List<GameObject>();
before, and it works perfectly ! Thanks a million
No problem :) should've definitely gotten to this sooner though but glad it works now finally haha.
Your answer
Follow this Question
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