Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

MonoBehavior events doesnt work

Discussion in 'Scripting' started by pankhard159, Jan 29, 2019.

  1. pankhard159

    pankhard159

    Joined:
    May 14, 2018
    Posts:
    13
    Hi,

    I'm beginner and I'm creating game for spawning objects on 2D tilemap. Spawned objects are only sprite renderer with C# script, rigidbox 2d and circle collider 2d (just for circle shape). So I'm in phase to use attached script to get OnMouseEvents but with maximal effort I'm not able to fix bug/feature to be able to call these events.

    I'm currently have scene with tilemap grid and canvas. Grid for tilemaps and Cavnas for menu selection. I'm able to raycast clicks on spawned objects from TileManager for certain purposes - right now for Debug.Log(). The click and a object spawned have nice intersection. But I want to be able to catch these events separated on every spawned object.

    Unity hierarchy:
    https://ibb.co/p3QcyXg

    Object properties:
    https://ibb.co/Y3nKP0s

    Script in spawned object:

    Code (CSharp):
    1.  
    2. public class TileResource: MonoBehaviour
    3. {
    4.     public GameObject go; //own object where the script is attached
    5.  
    6.     void OnMouseDown()
    7.     {
    8.        Debug.Log("CLICK...");
    9.     }
    10. }

    I'm not able to resolve it. Any idea post it please? Enabling IsTriggered doesnt help.

    I have also checked this https://docs.unity3d.com/Manual/ExecutionOrder.html. I didn't tried all but most of it.
     
    Last edited: Jan 29, 2019
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Make sure you have an EventSystem in the scene? Make sure "Raycast Target" is checked on the UI element?
     
  3. pankhard159

    pankhard159

    Joined:
    May 14, 2018
    Posts:
    13
    Did u make sure that u have seen the hierarchy ? Raycast is working with every single element in scene but the functions On...() is not able to call on the spawned object. For example I'm able to raycast the spawned object from TileManager with good response.
     
    Last edited: Jan 30, 2019
  4. pankhard159

    pankhard159

    Joined:
    May 14, 2018
    Posts:
    13
    Solved: Event Trigger on spawned object was missing. Select proper event and go.