1
\$\begingroup\$

I created "main script" and in Start() created simple cube:

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale = new Vector3 (1,2,1);
cube.GetComponent<Renderer>().material.color = new Color(0,0,255);
cube.AddComponent<CubeScript>();

and then in CubeScript I have empty Start() and Update() but in onMouseDown() just Debug.Log("click detected");

and I can never see log despite clicking on cube.. Any advice? did I missed something?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Sorry, but you have a typo in Addcomponent it should be AddComponent \$\endgroup\$ Commented Feb 16, 2016 at 11:59
  • \$\begingroup\$ thanks. it is corrected ;) didn't copy pasted it from the script \$\endgroup\$
    – Ewoks
    Commented Feb 16, 2016 at 12:21
  • \$\begingroup\$ Now your code should work fine. Make sure other things from inspector \$\endgroup\$ Commented Feb 16, 2016 at 12:47
  • \$\begingroup\$ Code was already with AddComponent.. what other things to make sure from inspector? didn't understand that.. :S \$\endgroup\$
    – Ewoks
    Commented Feb 16, 2016 at 13:26
  • \$\begingroup\$ can you post the script of CubeScript and screenshot of new created cube's inspector? \$\endgroup\$ Commented Feb 16, 2016 at 13:46

2 Answers 2

2
\$\begingroup\$

problem actually was that I typed onMouseDown() instead of OnMouseDown() .. Everybody coming from Java world pay attention!!! MonoDevelop will not warn you that mathod with such a name doesn't exist, but it will just silently ignore the issue.

Thanks a lot to @Malrig & @Hasan .. There were really trying to help me and made me double check what I typed

\$\endgroup\$
2
  • 2
    \$\begingroup\$ By the way there is nothing to warn. If you don't write correct spellings of MonoBehaviour's messages then compiler will consider that a new method, like common other own created methods. \$\endgroup\$ Commented Feb 16, 2016 at 14:27
  • 1
    \$\begingroup\$ Just to clarify, it doesn't warn that method doesn't exist because you actually just created it. I'm not sure why these aren't virtual functions though, that way intellisense could show you the valid methods just by typing 'override' .. oh well. \$\endgroup\$
    – Alfie
    Commented Feb 16, 2016 at 14:27
1
\$\begingroup\$

Your cube doesn't have a collider.

Try

cube.AddComponent<BoxCollider>();

See: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

\$\endgroup\$
2
  • 2
    \$\begingroup\$ That was my first thought. But the thing is, collider is already attached when you create primitive like this :) \$\endgroup\$ Commented Feb 16, 2016 at 13:18
  • \$\begingroup\$ exactly as Hasan said.. I tried to add BoxCollider2d and got warning that collider is already attached \$\endgroup\$
    – Ewoks
    Commented Feb 16, 2016 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .