So, in my little program, I’m making clickable objects. This is all well and good. I’ve got an empty game object with a script which puts a Raycast2D at my mouse’s click. From this I can obtain the object that was clicked. However, I have run into trouble when trying to set variables on clicked objects.
I want all clickable objects to have a script containing a Boolean “clicked” which I can set and then use for various purposes. my problem is I don’t know how to do this. Any help would be greatly appreciated.
My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Click_Controller : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//try to click something
if (Input.GetMouseButtonDown(0))
{ Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero); //send raycast
if (hit.collider != null) //Did it hit anything?
{
hit.collider.gameObject ; //Here I want to set the boolean "clicked" to true in the script "clickable" but it wont let me becuase they are as of yet undefined
}
}
}
}