- Home /
How can i update a list in update function only when there is a change ?
In the top of the class i added: [ExecuteInEditMode]
private void Update()
{
_anim = GetComponent<Animator>();
foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips)
{
animations.Add(ac);
}
}
So now it will add to the List all the time a value the same value . The List animations is public. I want that if there is a new item that is not in the list yet only then add it to the list.
you can make Boolean variable to update the list in Update(). I would recommend you to use the Unity event for update list. $$anonymous$$ake Unity Event and whenever change detected refresh the list in his listener.
What event it will be ? Could you show me some example please ?
you have a UpdaEvent and you are listening in the start method, lets suppose we want to update list in OntriggerEnter $$anonymous$$ethod just invoke the UpdateEvent
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ListUpdate : $$anonymous$$onoBehaviour
{
public UnityEvent UpdateEvent;
public List<Animation> AnimationClipList = new List<Animation>();
void Start () {
UpdateEvent.AddListener(delegate
{
var temList = new List<Animation>();
foreach (var animationClip in AnimationClipList)
{
temList.Add(animationClip);
}
AnimationClipList.Clear();
AnimationClipList = temList;
});
}
private void OnTrigerEnter(Collider collider)
{
//lets suppose you want to update list here
UpdateEvent.Invoke();
}
}
Answer by HarshadK · May 09, 2017 at 11:35 AM
All you need to do is perform a List.Contains(T) check to decide if item should be added to the list or not. Your code will be:
private void Update()
{
_anim = GetComponent<Animator>();
foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips)
{
// Check if animation is already present in the list or not and then add only if it is not present.
if(!animations.Contains(ac)) {
animations.Add(ac);
}
}
}
This will work when adding a new animation clip to the Animator window. But i also want that when i remove from the Animator window animation clip that it will update the list too and remove the item/s.
Hi Chocolade, have you found a way out on how to do it??
Answer by bharath09vadde · Apr 11, 2018 at 01:30 PM
Hi Chocolade, have you found any ways on updating a list??
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers