Hello again.
For my game I am trying to get my clickToMove script to access my targeting script so instead of when you right click an enemy and it targets and then continues to walk to the location that you click, instead it takes the target from the other script and walks in front of the target instead. The problem is I don’t know how to access the variable on the other script.
The game component is a sprite.
I have checked quite a few different answers but none of them seem to work or apply to what I’m trying to do.
Here is the target select bit:
private Transform selectedTarget = null;
void Update(){
if (Input.GetMouseButtonDown(1)){ // when button clicked...
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
//Debug.Log (pos);
RaycastHit2D hit; // cast a ray from mouse pointer:
hit = Physics2D.Raycast (pos, Vector3.zero);
if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
DeselectTarget(); // deselect previous target (if any)...
selectedTarget = hit.transform; // set the new one...
SelectTarget(); // and select it
}
}
}