My variable ‘target’ is set to my minion’s transform in addTarget.
Next I have doButton which assigns the target’s MoveControl to a button on the screen.
Then when I click the button on the screen I have a null reference error.
I made the target a public transform so I could see in the inspector if it changed after the ‘addTarget’ function; and it does not change!
One last thing that may be relevant, when I step through the debugger, target is set to the minion’s transform after the addTarget function. That persists through the doButton function, but after that function it seems to reset back to null. Why would this be happening?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Selection : MonoBehaviour {
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void AddTarget(Transform t)
{
target = t;
//transform.Find("Button");
}
public void SelectMe(Transform t)
{
AddTarget(t);
doButton();
}
Button b0;
void doButton()
{
if (target = null ) { return; }
Transform t1;
Transform t2;
Transform t3;
Button b;
t1 = transform.Find("Canvas");
t2 = t1.Find("Panel");
t3 = t2.Find("Button");
b = t3.GetComponent<Button>();
//b0 = transform.Find("Button").GetComponent<Button>();
b0 = b;
b0.onClick.AddListener(() => SetupUpButton());
}
void SetupUpButton()
{
target.GetComponent<MoveControl>().MoveUp();
Debug.Log("MoveUp");
}
}