OnMouseDown event for an Instantiated object.

I do not want to have to add a script to acheive this. I was expirementing to see if this would work:

function Start(){
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3(0, 0.5, 0);
cube.OnMouseDown(){ObjClick(cube);}}

function ObjClick(obj){
Debug.Log(obj)}

but it didn’t. :smile:

Is there any way I can do this? Any help would be appreciated. 8)

You must add a script.

OnXXXX in Unity are message receivers.
So either a MonoBehavior with the function is present or it will not work

Meh…

Ok then how could I add a script to a CreatePrimitive object? :cry:

through AddComponent adding your own monobehavior extending script (or in case of js, just the script)

CreatePrimitive creates a game object for you, so no problem

Okay I got ANOTHER question with this, how can I refer to the GameObject parent of the script? This won’t work…

function OnMouseDown () {
if (EditorMain.Selected) {EditorMain.Selected.renderer.material.color.g -= 10;}
renderer.material.color.g = 10;
//works fine till here!
EditorMain.Selected = GameObject;//Editormain.selected is a GameObject static value in another script
}

EDIT: Nevermind it didn’t work because I capitalized the G in GameObject…