There is a code:

private GameObject PastObj; private GameObject[] AllUsing; public int Offset; private void Awake() { if (name != "1") { AllUsing = GameObject.FindGameObjectsWithTag("CatUpgradesParent"); foreach (GameObject Obj in AllUsing) if (Obj.name == (Int32.Parse(name) - 1).ToString()) PastObj = Obj; Debug.Log(name + " | " + GetComponent<RectTransform>().anchoredPosition.y + " - " + PastObj.GetComponent<RectTransform>().anchoredPosition.y + " obj " + PastObj.name + " " + PastObj.transform.root.GetComponent<CatUpgrade>().islight); } } private void Update() { if (PastObj.transform.root.GetComponent<CatUpgrade>().islight) Offset = -50; else Offset = -150; if (name != "1" && GetComponent<RectTransform>().anchoredPosition.y - PastObj.GetComponent<RectTransform>().anchoredPosition.y != Offset) transform.Translate(new Vector2(0, 1) * Time.deltaTime); } 

In the array AllUsing elements named:

  • one
  • 2
  • 3
  • four

In the Awake() method, foreach should from all elements of the AllUsing array find the one whose name corresponds to the name of the element on which the script is 1. But PastObj is always empty. Why?

UPD: This script hangs on all objects of the AllUsing array and all of them are tagged with CatUpgradesParent

  • Do you have this script in single player? Or does it hang including on the object with the name 1? - M. Green
  • It hangs on all the objects of the AllUsing + array for all these elements. The tag is CatUpgradesParent @ M.Green - Andrew

1 answer 1

PastObj link is not always empty. It is empty only for the object with the name "1". In Awake (), you perform a search and assignment in PastObj for all instances of a class, except for the one that hangs on an object named "1".

And in the Update () loop, even before checking the name, you are already trying to access its transformation via the PastObj link.

Because in the script on object "1" in Awake (), the link was not assigned, and at the beginning of Update you do not check the link or name before the Update, then you will get an exception.