Send a message to an object when hovered/clicked/rightclicked

I am making a project that contains of a grid of 200 x 200 tiles.
I need a way to send a message to the tile when being hovered on. The message will contain the Sprite Variable so it will change the texture whenever the tile will recieve the message.

TilePlacer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TilePlacer : MonoBehaviour {

    //textures Go Here!

    public Sprite Texture_1_Dirt;

    //textures End Here!


    public Object Camera;
    public int Dimensions = 200;


    // Use this for initialization
    void Start () {
        for (int TileX = 0; TileX < Dimensions; TileX++) {
            for (int TileY = 0; TileY < Dimensions; TileY++) {

                GameObject TileObject =  new GameObject ();    //creates the object

                TileObject.name = "Tile " + TileX + "," + TileY;    // Names the object

                SpriteRenderer TileSpriteRenderer = TileObject.AddComponent<SpriteRenderer> ();  //adds Sprite renderer
                TileObject.transform.position = new Vector3 (TileX , TileY ,0); // moves its position to place
                TileObject.AddComponent<Tile> ();


                TileObject.SendMessage ("TileUpdate",Texture_1_Dirt);

                print ("Made tile " + TileX + "," + TileY);
                
            }
        }
    }
}

Tile

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tile : MonoBehaviour {

    SpriteRenderer Sprite_Render;


    void TileUpdate(Sprite TileSprite){

        print ("Request to Update the tile has been recieved for tile:" + transform.position);

        Sprite_Render = GetComponent<SpriteRenderer>();
        Sprite_Render.sprite = TileSprite;

    }
}

CameraMovement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour {

    //textures Go Here!

    public Sprite Texture_1_Dirt;

    //textures End Here!




    public int Dimensions = 200;


    // Use this for initialization
    void Start () {

   

    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetKey ("w")) {
            transform.position = new Vector3 (transform.position.x, transform.position.y + transform.position.z / 5, transform.position.z);
        }

        if (Input.GetKey ("s")) {
            transform.position = new Vector3 (transform.position.x, transform.position.y - transform.position.z / 5, transform.position.z);
        }

        if (Input.GetKey ("d")) {
            transform.position = new Vector3 (transform.position.x - transform.position.z / 5, transform.position.y, transform.position.z);
        }

        if (Input.GetKey ("a")) {
            transform.position = new Vector3 (transform.position.x + transform.position.z / 5, transform.position.y, transform.position.z);
        }

        transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z - (Input.GetAxis("Mouse ScrollWheel")*10));


        if (transform.position.z > Dimensions) {
            transform.position = new Vector3 (transform.position.x, transform.position.y, Dimensions);
        }
    }
}

BTW: C sharp script only.

Since you have a tile class, it would probably be easiest to use
OnMouseOver

@fire7side but the tiles will be touching next to one another so technically the mouse will already be over something. So on mouse over would run like the update function

If you don’t like OnMouseOver() approach, you can always do a raycast to from the camera to the mouse click point, and determine what tile it’s been clicked on.

OnMouseEnter/OnMouseExit?

1 Like

@VergilUa I’ve already tought of that but I dont really know how to send a message to the hit object.

@Chris-Trueman On Mouse exit would never trigger because the tiles are touching each other so the mouse will always be on something.

This tells me that you need to do some of the basic tutorials, sending information between scripts is something you should grasp before attempting stuff like this. These should give you a good start:

and

https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system?playlist=17117

Please check the titles of the tutorials to see if your question is already dealt with before posting on here, the same questions seem to get asked every day.

I’d recommend to not use the OnMouseXYZ callbacks. They’re old and may introduce undesired behaviour when you start to add UI elements to your scene, as those do not block it. That is, any action on UI elements will as well trigger OnMouseXXX actions on objects that are actually hidden behind the UI elements.

You can of course workaround that by quering the currently hovered object and everything else you need to know about the current situation, but you alwas have to add that again and again wherever you need that distinction.

So, it’s much better to jump straight into the newer event systems and use the handler interfaces such as the IPointerClickHandler and all the others you can find there in the side-menu.
These handlers can also be configured using EventTrigger components.

In order to make them work on world-objects as well (not only UI elements), you’ll need the PhysicsRaycaster component on your camera.

For now, it may sound more complex and complicated, but once you start using UI elements, it’ll be much less complicated than all workarounds and cause less headaches.

@Suddoha Tnx I’ll try this tomorrow because today my laptop is gone for repairs.

I think you will need the EventSystem as well so if they don’t have any UI stuff it will need to be added.

They are different objects and will trigger their own messages, so yes it will.