- Home /
Correct way to have individual lists on different objects with same script
I have 4 objects that are referenced in the Notifications Script. Each object has A TESTNotificationObject script. On each call an NotificationClassType object should be added to the specific list. When you call that object you should be able to remove or select the specific object added. The problem is that the lists are acting very wierd, i don't know if it has something to do with a wierd memory allocation mistake that i have done or anything else but the problem is this: If i add an object to just one of my 4 GameObjects and then try to access the script, it fails. If i add one object to the list of each of the 4 objects, i can call the remove element just once. If i have more then one elements in each list i can call each list to remove elements until i have one remaining then it will tell me that the list is empty. So somehow even though i add the objects to the list, the first ones are not seen until i add a second one, the it sees them both.
public class TESTNotificationObject : MonoBehaviour
{
List<NotificationClassType> notificationTypeList = new List<NotificationClassType>();
private bool isListEmpty = true;
public void OnAddObject(NotificationClassType notification)
{
notificationTypeList.Add(notification);
if (isListEmpty == true)
isListEmpty = false;
}
public void OnRemoveObject()
{
if(notificationTypeList != null)
notificationTypeList.RemoveAt(0);
if (notificationTypeList.Count == 0)
{
isListEmpty = true;
//start animation
}
}
public void OnSelectObject()
{
if(notificationTypeList != null)
{
notificationTypeList[0].LerpToPosition();
notificationTypeList[0].PrintNames();
OnRemoveObject();
}
}
public void ActivateNotificationIcon()
{
Debug.Log("Notification Icon Activated");
//start animation
}
public void DeactivateNotificationIcon()
{
}
}
public class Notifications : MonoBehaviour
{
public GameObject gameCamera;
public bool isNotificationSelected = true;
public float notificationTimer = 0f;
public float timeToMove = 1f;
[SerializeField]
[Range(0f, 1f)]
private float lerpPct = 0.5f;
public GameObject DuelResultObjectRef;
public GameObject CommanderInjuredObjectRef;
public GameObject BattlefieldActivatedObjectRef;
public GameObject UnitBehaviourChangedObjectRef;
public void AddToDuelResultList(NotificationClassType obj)
{
if(DuelResultObjectRef.activeSelf == false)
{
DuelResultObjectRef.SetActive(true);
DuelResultObjectRef.GetComponent<TESTNotificationObject>().ActivateNotificationIcon();
}
DuelResultObjectRef.GetComponent<TESTNotificationObject>().OnAddObject(obj);
}
public void AddToCommanderInjuredList(NotificationClassType obj)
{
if (CommanderInjuredObjectRef.activeSelf == false)
{
CommanderInjuredObjectRef.SetActive(true);
CommanderInjuredObjectRef.GetComponent<TESTNotificationObject>().ActivateNotificationIcon();
}
CommanderInjuredObjectRef.GetComponent<TESTNotificationObject>().OnAddObject(obj);
}
public void AddToBattlefieldActivatedList(NotificationClassType obj)
{
if (BattlefieldActivatedObjectRef.activeSelf == false)
{
BattlefieldActivatedObjectRef.SetActive(true);
BattlefieldActivatedObjectRef.GetComponent<TESTNotificationObject>().ActivateNotificationIcon();
}
BattlefieldActivatedObjectRef.GetComponent<TESTNotificationObject>().OnAddObject(obj);
}
public void AddToUnitBehaviourList(NotificationClassType obj)
{
if (UnitBehaviourChangedObjectRef.activeSelf == false)
{
UnitBehaviourChangedObjectRef.SetActive(true);
UnitBehaviourChangedObjectRef.GetComponent<TESTNotificationObject>().ActivateNotificationIcon();
}
UnitBehaviourChangedObjectRef.GetComponent<TESTNotificationObject>().OnAddObject(obj);
}
}
Do you assign each of the 4 GameObjects (DuelResultObjectRef and so on) from the Inspector? Are all of them referring to a unique GameObject with a TESTNotificationObject component?
What do you do when you "try to access the script"? How does it fail? Also I would add debug logs to OnAddObject and OnRemoveObject printing the list count.