In my program I am storing the “level” of a certain “monster” in LogicScript, which is attached to a game object called LogicManager. This “level” is changed by clicking on the up/down buttons as well as buttons that change the levels of every monster at once:
this is not a screenshot of the whole screen, but it contains every relevant component

The up button contains UpButtonScript, which contains all the methods necessary to update the level.
The “add all 1” button contains SetAILevelScript, which loops through all the monsters and uses their respective UpButtonScripts to update the levels.
When clicking the up/down button for each individual “monster”, everything works as intended. But when I click the “add all 1” button, it only functions if I had clicked the individual up button first. Otherwise it gives me the following error:

The error points to this in UpButtonScript:

I am guessing that Unity thinks logicMan is null, even though it is instantiated in start(), and the incrementButton() method works when I click the up arrow directly. As said before, clicking “add all 1” doesn’t work when it is clicked first. Here is the start() in UpButtonScript, where it instantiates logicMan:

Sorry if this is a lot but I wanted to make sure I included all the relevant information up front.
UpButtonScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class UpButtonScript : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private bool buttonPressed;
private int level;
private string monsterName;
private GameObject logicMan;
public GameObject aiButton;
public Button upButton;
public TMP_Text levelText;
void Start()
{
// find the logicManager gameObject
logicMan = GameObject.FindGameObjectWithTag("LogicManager");
// set the level and buttonPressed to default (false)
level = 0;
buttonPressed = false;
// get the name of the AI this button is linked to
monsterName = aiButton.gameObject.name;
}
private IEnumerator tickUp()
{
while (buttonPressed)
{
level = logicMan.GetComponent<LogicScript>().getLevel(monsterName);
if (level == 0)
{
showButton();
}
incrementButton();
yield return new WaitForSeconds(0.1F);
}
}
public void showButton()
{
aiButton.GetComponent<Button>().interactable = true;
levelText.gameObject.SetActive(true);
}
public void incrementButton()
{
if (level < 20)
{
level = logicMan.GetComponent<LogicScript>().getLevel(monsterName);
updateButton(++level);
}
}
public void updateButton(int level)
{
logicMan.GetComponent<LogicScript>().setLevel(monsterName, level);
levelText.GetComponent<LevelTextScript>().updateText(level.ToString());
}
public void OnPointerDown(PointerEventData eventData)
{
buttonPressed = true;
StartCoroutine(tickUp());
}
public void OnPointerUp(PointerEventData eventData)
{
buttonPressed = false;
}
}
SetAILevelScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SetAiLevelScript : MonoBehaviour
{
public Button button;
public GameObject[] aiButtons;
private int delta;
// Start is called before the first frame update
void Start()
{
button.onClick.AddListener(TaskOnClick);
aiButtons = GameObject.FindGameObjectsWithTag("AiButton");
switch (button.gameObject.name)
{
case "Set0":
delta = 0;
break;
case "Add1":
delta = 1;
break;
case "Set10":
delta = 10;
break;
case "Set20":
delta = 20;
break;
}
}
// Update is called once per frame
void Update()
{
}
private void TaskOnClick()
{
foreach (GameObject aiButton in aiButtons)
{
GameObject downButton;
GameObject upButton;
if (delta == 0)
{
downButton = aiButton.transform.GetChild(1).gameObject.transform.GetChild(1).gameObject;
upButton = null;
}
else
{
downButton = null;
upButton = aiButton.transform.GetChild(1).gameObject.transform.GetChild(0).gameObject;
}
switch (delta)
{
case 0:
downButton.GetComponent<DownButtonScript>().hideButton();
downButton.GetComponent<DownButtonScript>().updateButton(0);
break;
case 1:
upButton.GetComponent<UpButtonScript>().showButton();
upButton.GetComponent<UpButtonScript>().incrementButton();
break;
case 10: case 20:
upButton.GetComponent<UpButtonScript>().showButton();
upButton.GetComponent<UpButtonScript>().updateButton(delta);
break;
}
}
}
}

