Getting component info on a clicked object

Hey guys,

How do I get the info of a component in an object I just clicked on?

I’ve been trying to use the OnMouseDown function and doing a collider.transform.renderer.material.color (I’m trying to get the material color on the gameObject I clicked on) but it’s not working.

Any ideas?

Thanks!

Please post your code.

If it can helps you: Create a cube, drag the following script on the cube, press play, click on the cube and you’ll see the Debug lines.

public class OnClick : MonoBehaviour {
	
	void OnMouseDown() {
		Debug.Log("clicked on: "+gameObject.name);
		Debug.Log(gameObject.renderer.material.color);	
	}
}

Please format your code next time. It makes it easier to offer good feedback (use the Php tags in the full editor for some basic colors too).

Your code looks fine, except for the empty start and update functions (pointless overhead but won’t break your moues event). I’ve been using EZGUI for so long I kind of forget how this works. i glanced at the docs and saw this:

(Unity - Scripting API: MonoBehaviour.OnMouseDown())

You mentioned something about a collider in your first post, but your code is accessing a gameObject property. For clarity, is the code you posted in a script on the same game object you are clicking, and does it also have a collider component?

EDIT: Just noticed the OP is not the one who posted the code. Opps =)

Thanks a lot getamac… I’ll play with the snippet you posted.

Rafes the 3rd post is from getamac, not me, but here’s my code so far:

var mouseClick:Vector2;

var mouseSnapshot:Vector2;

var mouseDrag:boolean = false;

var mouseCompare:Vector2;

var thisColor:Color;



private var hoverInfo = new Array();          // Information where the mouse is currently hovering







function Start(){





}



function Update(){



	

	//Show the current mouse position in the inspector:

	mouseClick = getMousePos();

	// Handle mouse button down event

    if(Input.GetMouseButtonDown(0))

        storeMouseLoc();

        //getColliderInfo();

    // Handle mouse drag event

    if(Input.GetMouseButton(0))

        dragCheck();

        

    // Handle mouse up event

    if(Input.GetMouseButtonUp(0))

    

    	mouseDrag = false;

    

	

}





//Get the current mouse position

function getMousePos(){

    var pos : Vector2 = new Vector2(Input.mousePosition.x, (Screen.height - Input.mousePosition.y));

    return pos;

}



//Stores current location on mouse down

function storeMouseLoc(){

	

	hoverInfo = new Array();

	hoverInfo.Push(getMousePos());

	Debug.Log(hoverInfo[0]);

	mouseSnapshot = hoverInfo[0];

	

}



// Checks for mouse dragging

function dragCheck(){

    if(hoverInfo.length > 0)

        if((getMousePos() - hoverInfo[0]).sqrMagnitude > 60) mouseDrag = true;

}



function OnMouseDown(collision:Collision){



	thisColor = 



}

The problem I’m having is at that last line of code. This script I created just to debug a few things related to the mouse event, so I can see the feedback on the inspector at game time.

Cheers!

Ah OK. Well use a simple test like …

Debug.Log("here");

Are you looking for…?

collision.gameObject.renderer.sharedMaterial.color

(I didn’t test this. You may be able to omit ‘gameObject’ since that is just another component, but I’m not sure of colliders have that exposed or not.)

P.S. Thanks for posting code in color, but please do clean it up a bit more. I know it takes time but we are just volunteers too ;)… I just looked at the last line because that is what you called attention to.

Hey Rafes,

Thanks for the help. Unfortunately that didn’t work… I’ll keep playing with it. :slight_smile:

What do you mean by “didn’t work”? Did you get an error message? If so, copy and paste it.

No error message, but I have the color var showing up in the inspector and it’s not changing to the selected component’s color as I expected it to.

The thing is that I thought that the OnMouseDown function would get the information of the collider being clicked on as a parameter so that I can access that collider’s component as in: collider.transform.renderer.material.color. I also tried shared materials as you indicated but it also didn’t work. The whole point of this script is for me to learn how the mouse events work.

Cheers.

All you need to test anything is a Debug.Log() message. When you click, just have it print “Click”.

I see what you mean now. The method doesn’t provide an argument for you to use. Even if it did, to be more accurate, it would be giving you a reference to the collider component which is on the “other” GameObject. Once you have that you can navigate anywhere you want. However, since this script has to be on the same object as the collider, it wouldn’t make much sense to give you a reference to the collider, since you are already on that GameObject. You have full access already.

getamac posted code which should work for you. I noticed the docs say: Renderer Inherits from Component, so I don’t even think you need ‘gameObject’. ‘renderer’ should already be exposed.

So when you try his code, it doesn’t log color info?

BTW, “sharedMaterial” is the actual material asset that all instances reference while just “material” is that particular instance’s material reference. They are the same unless you edit the instance material, in which case it will no longer batch)

Rafes, this script in on an empty gameobject. I want to click on the object and get it’s properties. I want to be able to click on any gameobject which has a material and get it’s color information from this one script on this one empty gameObject.

I hope I’m able to explain what I’m trying to do clearly, sorry for the confusion.

Again, thanks for the help.

Cheers.

Oh ok. I thought you were having trouble with the script. You don’t want to use this method because, as the docs say, you need to have a script on the game object which is clicked to make it work.

What you want to do is cast a ray from the camera, filtered by the layers you want to hit (just to keep things clean), then when you hit something, send it an optional message

 target.SendMessage("OnSelected", SendMessageOptions.DontRequireReceiver)

This is the perfect case to use send message because it won’t happen often (only as fast as someone can click, which is nothing in nearly all cases), and it is optional, so if the clicked object (the one with the collider that was clicked) has a function OnSelected() it will trigger, otherwise nothing will happen and no error will be thrown.

The name OnSelected is my own. You can use any name you want. I like adding the word “On” before events though. it is a clean paradigm.

You can use BroadcastMessage instead if you want to automatically send to children of the hit collider as well.

You can also send a single argument. Unity - Scripting API: GameObject.SendMessage

For anyone who is reading this and is using EZGUI, you just need to add a hit delegate and if the event is a tap, grab the pointer.hitInfo.collider.transform. From that point on it is just like I explained above, you just don’t need to cast the ray and there are more options built in.

[Duplicate Deleted]

Rafes thanks a lot for your help and patience.

I’ll try it as soon as I get back on my machine back home. (at work ATM) and I’ll post the result.

Cheers!

Rafes, thanks a lot man… it worked fine :slight_smile:

BTW, very interesting Asset Packages you have there, I’ll have to check them out sometime!

Cheers!

Np, glad to help!