Detect mouse down on multiple objects from single script?
I need a way to link mutliple GameObjects to a single script and tell which one is being clicked, lets say i have 3 cubes, object 1 2 and 3 now normally i know the public void MouseDown but how do i make multiple of those and link them to different objects to do different things, sorry if this is a noob question but i honestly just get stuck sometimes
Answer by Inok · Oct 08, 2016 at 09:07 AM
Try Event Trigger
So add Event Trigger component to each of cubes gameobjects and choose "PointerDown" or "PointerClick" (what more match your behaviour) then choose gameobject that contain your logic script and assign function you want. So make for each cube different function and assign from event trigger and as result when you click on that cube you call assigned function that do what you want. Similar you can do but without asign in inspector but i not know how do all this only as code.
im not quite sure what you mean, im making a shop and there are buttons that will change certain things but i need to change the values of the buttons and what they do, they all do the same thing which is be clicked and run the commands but how do i change the values of those depending on what object the player is clicking?
lets create a scenario there are 3 buttons A, B and C if you press A it will take 15 health, B 30 and C 45 there is a script that has the public void mousedown and inside it has some code to take away the players health by the amount, but how to tell the difference between the buttons and which button was pressed?
Your answer
Follow this Question
Related Questions
How to keep an array of targets in detection radius? 0 Answers
How can i detect/check color of sprites ? 0 Answers
No overload for method 'fireBullet' takes 0 arguments 1 Answer
Object reference not set to instance of an object? 0 Answers
Object Follow Mouse Without Making The Collider Stops Working 1 Answer