Selecting An Object, Remembering It, Hiccups

Greetings!

Huge newbie here, working on my first non-tutorial project, but I’ve hit a snag and I was hoping I could enlist your help.

In my 2D game I have a set up screen before a battle where the player should be able to click on a tile to select it, then click on a unit button in a menu to place that unit on the selected tile. My answer to that begins with this code:

using UnityEngine;
using System.Collections;

public class SetUpController : MonoBehaviour {

    public Camera camera;
    public Vector3 TilePosition;

    void Start () {

    }
   
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
       
            if (hit.collider != null && hit.collider.tag == "UnitSquare")
            {
                TilePosition = new Vector3(hit.collider.gameObject.transform.position.x,
                                           hit.collider.gameObject.transform.position.y,
                                           hit.collider.gameObject.transform.position.z);
                Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
            }
            else if (hit.collider != null && hit.collider.gameObject.name == "Button")
            {
                Debug.Log ("Hit Button");
            }
        }
    }
}

The idea being I use the Raycast to discover where the tile is and save it’s Vector3 to TilePosition so that I can use it later when I need to instantiate the unit at the tile’s position (or based off the tile’s position anyway). This works out alright to my knowledge, at least the debug message seems to think so. Victory!

But I also want to use the same Raycast to determine if the Unit’s Button has been pressed, however my first hurdle comes there. The game doesn’t seem to recognize that the Unit’s Button has been clicked as I never receive the Debug message.

The button has a box collider, its name matches up with its name in the code, and the principle I used to detect if the tile was clicked is the same idea I’m trying to replicate with the button so it seems like it should work. Can I not use one Raycast for these different tasks? Am I barking up the wrong tree with this whole code? If not, what am I doing wrong? These are the thoughts that plague me!

My experience is very limited, I’m probably missing something incredible simple. Thank you in advance for any help you can offer.

all of this code happens in one frame, so hit can’t return both at the same time (you can’t be in two places at once etc.). If you want to record the button doing something you will need to store “something” to reflect that.

to build on that, lets say you have 5 units to be placed on the board. When you click on a unit the “placement manager” script needs to store the “current selected unit”, if you click again to deselect it needs to set it to null.

When you click on a grid cell it can then check “do i have a selected unit, if so place it and set current to nothing”. etc

Thanks for the help LeftyRighty, but maybe I can pick your brain a bit more? I’m a little confused about how I’m in two places at once. Could you point out exactly what I’m doing wrong in that regard? What I’m trying to accomplish (but obviously, not well) is to send out a single Raycast and then depending on what it collides with have the script perform different tasks.

Should I instead send out two different Raycasts at once, both checking for different things?

Please, forgive the double post! But I’ve encountered a new problem after having solved the old (the problem from above was that my UI elements didn’t exist in the game world, so the Raycast couldn’t collide with them. Thankfully, along with the new UI system is an events system that can handle little things like clicking for me without the need of a raycast).

The new problem is about this code:

public class Unhire : MonoBehaviour {

    public GameObject SetUpController;
    private GameObject clickedtile;
    private GameObject UNIT1;

    void Awake () {
    }
   

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    public void UnhireUnit () {
        //Accessing info on which tile was last clicked.
        clickedtile = SetUpController.GetComponent<SetUpController>().ClickedTile;

        //Destroying any units on that tile.
        if (clickedtile.transform.childCount > 0)
        {
            Destroy (clickedtile.transform.GetChild (0).gameObject);
        }

        //Looking for units and if it can't find them, setting their existance to false in the SetUpController.

        UNIT1 = GameObject.Find ("Unit1(Clone)");

        if (UNIT1 == null) 
        {
            SetUpController.GetComponent<SetUpController>().Unit1Exists = false;
            Debug.Log (SetUpController.GetComponent<SetUpController> ().Unit1Exists);
        }
        else if (UNIT1 != null)
        {
            SetUpController.GetComponent<SetUpController>().Unit1Exists = true;
            Debug.Log (SetUpController.GetComponent<SetUpController> ().Unit1Exists);
        }


    }

What I want to do is, with one button press, is have UnhireUnit destroy any children of the object ClickedTile and then search for all the gameobjects that could have possibly been its children (atm though, the script is only searching for one) and then if it can’t find any copies in the scene, change a bool attached to SetUpController to show that those gameobjects are no longer present.

The problem I’m having is if Unit1 is already a child of the ClickedTile and I click the button that runs UnhireUnit to destroy it (which it does just fine), it doesn’t switch the bool to false unless I click the button a second time. Neither of my two Debug.Log are giving me anything on the first click, nor is the bool actually switched. However on the 2nd click, the bool is changed and my Debug.Log reports as much.

I fear, as LeftyRighty mentioned with my other problem, I might be trying something with the if statements that I can’t actually do, but if that’s the case I haven’t been able to find a different way.

If there’s any advice you can give me on this, I’d very much appreciate it. Thanks in advance for your help.