As I mentioned in the title, my script that I wrote below, is giving me errors when I call LevelUp() method from a button in UI system. In the inspector, it is assigned. It all happens in the same script. The “unassigned” object is the selectedObject variable. I restarted Unity, look out for another instance of FactoryManager component, but none of it worked for me. I will very appreciate your help!
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FactoryManager : MonoBehaviour {
public List<GameObject> factories;
public int selectedFactoryId;
[SerializeField] private GameObject selectedObject;
[SerializeField] private GameObject gameManager;
[SerializeField] private LayerMask mask;
private GameObject factoryPanel;
GameObject moneyToNextLevelText;
GameObject factoryLevelText;
GameObject factoryNameText;
Image factoryLevelBar;
public void AddNewFactory(GameObject factoryToAdd)
{
factories.Add(factoryToAdd);
}
void Start ()
{
selectedFactoryId = 1;
gameManager = GameObject.FindGameObjectWithTag("GameManager");
factoryPanel = GameObject.Find("FactoryMenu");
moneyToNextLevelText = factoryPanel.transform.Find("LevelButton").Find("MoneyToNextLevel").gameObject;
factoryLevelText = factoryPanel.transform.Find("FactoryLevelText").gameObject;
factoryNameText = factoryPanel.transform.Find("FactoryName").gameObject;
factoryLevelBar = factoryPanel.transform.Find("LevelButton/Bar/Background").gameObject.GetComponent<Image>();
}
void Update ()
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero, Mathf.Infinity, mask);
if (hit.collider != null)
{
selectedObject = hit.transform.gameObject;
}
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero, Mathf.Infinity, mask);
if (hit.collider != null)
{
if (hit.transform.gameObject == selectedObject)
{
SelectFactory(selectedObject);
}
}
else
{
selectedObject = null;
}
}
}
}
public void SelectFactory(GameObject selectedFactory)
{
selectedFactoryId = selectedFactory.GetComponent<Factory>().factoryListId;
//Set factory panel values to current factory.
RefreshFactoryUI(selectedFactory);
//Open factory panel.
gameManager.GetComponent<FactoryMenu>().OpenFactoryMenu();
Camera.main.transform.position = new Vector3(Mathf.Lerp(Camera.main.transform.position.x, selectedFactory.transform.position.x + 1, 0.2f), 0, 0);
}
public void RefreshFactoryUI(GameObject selectedFactory)
{
factoryLevelBar.fillAmount = selectedFactory.GetComponent<Factory>().GetStageLevelRatio();
factoryNameText.GetComponent<Text>().text = selectedFactory.name;
factoryLevelText.GetComponent<Text>().text = "Level " + selectedFactory.GetComponent<Factory>().factoryLevel;
moneyToNextLevelText.GetComponent<Text>().text = "" + selectedFactory.GetComponent<Factory>().moneyToNextLevel;
}
public void LevelUp()
{
print(selectedObject.name);
}
}