Search Unity

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

Discussion in 'Scripting' started by wayne1512, Oct 23, 2017.

  1. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TilePlacer : MonoBehaviour {
    6.  
    7.     //textures Go Here!
    8.  
    9.     public Sprite Texture_1_Dirt;
    10.  
    11.     //textures End Here!
    12.  
    13.  
    14.     public Object Camera;
    15.     public int Dimensions = 200;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         for (int TileX = 0; TileX < Dimensions; TileX++) {
    21.             for (int TileY = 0; TileY < Dimensions; TileY++) {
    22.  
    23.                 GameObject TileObject =  new GameObject ();    //creates the object
    24.  
    25.                 TileObject.name = "Tile " + TileX + "," + TileY;    // Names the object
    26.  
    27.                 SpriteRenderer TileSpriteRenderer = TileObject.AddComponent<SpriteRenderer> ();  //adds Sprite renderer
    28.                 TileObject.transform.position = new Vector3 (TileX , TileY ,0); // moves its position to place
    29.                 TileObject.AddComponent<Tile> ();
    30.  
    31.  
    32.                 TileObject.SendMessage ("TileUpdate",Texture_1_Dirt);
    33.  
    34.                 print ("Made tile " + TileX + "," + TileY);
    35.                
    36.             }
    37.         }
    38.     }
    39. }
    40.  
    41.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tile : MonoBehaviour {
    6.  
    7.     SpriteRenderer Sprite_Render;
    8.  
    9.  
    10.     void TileUpdate(Sprite TileSprite){
    11.  
    12.         print ("Request to Update the tile has been recieved for tile:" + transform.position);
    13.  
    14.         Sprite_Render = GetComponent<SpriteRenderer>();
    15.         Sprite_Render.sprite = TileSprite;
    16.  
    17.     }
    18. }
    19.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraMovement : MonoBehaviour {
    6.  
    7.     //textures Go Here!
    8.  
    9.     public Sprite Texture_1_Dirt;
    10.  
    11.     //textures End Here!
    12.  
    13.  
    14.  
    15.  
    16.     public int Dimensions = 200;
    17.  
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.  
    22.    
    23.  
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.         if (Input.GetKey ("w")) {
    30.             transform.position = new Vector3 (transform.position.x, transform.position.y + transform.position.z / 5, transform.position.z);
    31.         }
    32.  
    33.         if (Input.GetKey ("s")) {
    34.             transform.position = new Vector3 (transform.position.x, transform.position.y - transform.position.z / 5, transform.position.z);
    35.         }
    36.  
    37.         if (Input.GetKey ("d")) {
    38.             transform.position = new Vector3 (transform.position.x - transform.position.z / 5, transform.position.y, transform.position.z);
    39.         }
    40.  
    41.         if (Input.GetKey ("a")) {
    42.             transform.position = new Vector3 (transform.position.x + transform.position.z / 5, transform.position.y, transform.position.z);
    43.         }
    44.  
    45.         transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z - (Input.GetAxis("Mouse ScrollWheel")*10));
    46.  
    47.  
    48.         if (transform.position.z > Dimensions) {
    49.             transform.position = new Vector3 (transform.position.x, transform.position.y, Dimensions);
    50.         }
    51.     }
    52. }
    53.  

    BTW: C sharp script only.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    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.
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    OnMouseEnter/OnMouseExit?
     
    xVergilx likes this.
  6. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    @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.
     
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    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:

    https://unity3d.com/learn/tutorials...etween-scripts-and-gameobjects?playlist=17117

    and

    https://unity3d.com/learn/tutorials...eating-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.
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  9. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    @Suddoha Tnx I'll try this tomorrow because today my laptop is gone for repairs.
     
  10. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I think you will need the EventSystem as well so if they don't have any UI stuff it will need to be added.
     
  11. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    They are different objects and will trigger their own messages, so yes it will.