[Getting to grips with C#] Passing bool from a clicked mesh.

Hi

I’m trying to pass a boolean from OnMouseDown but it appears unresponsive.

I suspect this to be the fact I don’t know a whole lot of C# but I’m trying to get this script to update X to true when I click the plane the script is attached to:

public class Test : MonoBehaviour
{

		bool X;

		void OnMouseDown ()
		{
	
				X = true;

		}

		void Update ()
		{
	
				Debug.Log (X);

		}
}

But nothing happens. Any ideas? Is there something specific I need to do to refresh/update the mouse click ?

Steps to replicate:
01 - Open Unity
02 - Create Cube
03 - Attach Script to Cube
04 - Activate play mode
05 - Click Cube
06 - Observe the Console and feel silly.

All answers and opinions appreciated

Passing a variable is one thing; what you are currently doing is assigning a value to one.

The script is simple and straightforward enough that there really shouldn’t be anything wrong with it. I’d make bool X public so that you could see its value in the inspector instead of having it constantly outputted in the console, but it should still be working.

Verify that you are actually building successfully without any errors - I assumed you left this out in the snippet, but you should have a using UnityEngine; statement at the top. Check that your cube is actually active and has an enabled collider component, which it should if you simply created it via the Unity menu.

That’s all I can think of - if you’re still having problems I’d be willing to look at the actual project if you attach it.

Check to make sure you have a box collider attached to the cube.

Excellent, that did the trick. Had a non collidable “selection plane”, when clicked it updates the grid position active/inactive boolean, Making it collidable updates as expected. I guess clicks are collisions in the world of Unity semantics.

Thanks!

You are correct! “Clicks” are really RayCasts searching for a collider from the camera into the scene.