- Home /
Calling a method using delegate makes my object null
Hi I tried making a event delegate that calls function A where function A repeatedly calls function B using a reference. Whenever function A gets called using the event delegate, at the first call for function B a variable in function B (Variable B) is not null. The problem starts at the second loop call of function B, because variable B becomes null. The weird part about it is that I tried not using the event delegate to call for function A, and called it using a reference instead and it works so that variable B does not return null on the succeeding calls. Do delegates somehow remove objects?
Here is my delegate located in Script A:
public delegate void DelegateEventHandler(EntityCard p_card, List<EntityCard> p_generalList, int index);
public static event DelegateEventHandler filterDelegate;
public static void DelegateCall(EntityCard p_card, List<EntityCard> p_generalList, int index)
{
if(filterDelegate != null)
{
filterDelegate(p_card, p_generalList, index);
}
}
It is called in the same script (Script A) using:
foreach (var card in m_currentFilterList)
{
DelegateCall(card, m_groupedList, i);
}
Script B is registered to the delegate with function A:
void OnEnable(){
CardFilter.filterDelegate += this.FunctionA;
}
void OnDisable(){
CardFilter.filterDelegate += this.FunctionA;
}
public void FunctionA(EntityCard p_card, List<EntityCard> p_generalList, int index)
{
GameObject gridCards = m_cardPooler .FunctionB (containerName [index]);
}
Function B in Script C is called by Function A in Script B:
public Queue[] VariableB;
public GameObject FunctionB (string p_containerType)
{
for(int i=0; i < m_containers.Length; i++)
{
if(m_containers[i].containerType == p_containerType)
{
if(VariableB == null)
{
Debug.Log("NULL QUEUE LIST");
}
else{
Debug.Log("HAS QUEUE LIST");
}
return something;
}
}
return null;
}
Your answer
Follow this Question
Related Questions
Reloading Scene causes "null" object references. 3 Answers
Delegates not working on iOS 0 Answers
How to unsubscribe from InputSystem event properly 0 Answers
NGUI UICenterOnChild, find out what is centered 1 Answer
Can't include System.Threading.Tasks 3 Answers