- Home /
OnMouse--- functions not working
I've already checked many threads on this issue and tries everything suggested there. It just doesn't work. The idea: I have a group of 8 spheres attached to one cube positioned between them and 8 point lights attached to these spheres. During the game when player holds any button (let's say, LeftShift), this group appears at the current position of mouse and after that moves with the camera so that the cursor can move separately from the group. When the player releases LeftShift, the group disappears. Here's the code for this script and it works perfectly:
public class GroupMove : MonoBehaviour {
private Vector3 pos;
private Vector3 oldpos;
private bool appeared;
public Camera cam;
public GameObject Player;
// Use this for initialization
void Start () {
oldpos=transform.position;
appeared=false;
}
// Update is called once per frame
void LateUpdate () {
pos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
if(Input.GetKeyDown(KeyCode.LeftShift) & appeared == false){
transform.position=cam.ScreenToWorldPoint(pos);
appeared=true;
}
else if(Input.GetKeyUp(KeyCode.LeftShift)){
transform.position=oldpos;
appeared=false;
}
}
}
Next idea: during this group appearance all the lights are disabled. If player drags the mouse over any of the spheres, the attached light gets enabled. During the test all the lights are shut down so the Start () function works. However, they don't react to the mouse. The script is attached to each sphere, they all have colliders that don't cross each other or lay on each other in front of the camera view. I even tried to separate them from each other and from their lights really far - no result. Tried functions Over, Enter, Exit, Click. Also added debug command to know wether or not they detect the mouse. Empty result. The sphere code:
public class ButtonCheck : MonoBehaviour {
public Light BtnLight;
void Start () {
BtnLight.enabled=false;
}
void OnMouseOver () {
Debug.Log ("Clicked!");
BtnLight.enabled=true;
}
}
I really need help on this one cause otherwise the project can be simply thrown away.
Tried that light script and it works. When the mouse goes over the sphere the light I setup works. So are you getting any errors in the console?
The console is empty and shows nothing. Could you please share a screenshot of how you positioned the group objects?
Sure @Unique$$anonymous$$AX, I only added 1 sphere and 1 pointlight just above as we just need to work out what's wrong.
and then the Inspector for the sphere that has the script on it:
Press play and move the mouse over the sphere and the light does com on.
EDIT
This is the script just for completeness:
using UnityEngine;
using System.Collections;
public class ButtonCheck : $$anonymous$$onoBehaviour {
public Light BtnLight;
void Start () {
BtnLight.enabled=false;
}
void On$$anonymous$$ouseOver () {
Debug.Log ("Clicked!");
BtnLight.enabled=true;
}
}
$$anonymous$$ake sure your colliders are enabled to get the On$$anonymous$$ouseOver function to be fired.
found the problem. $$anonymous$$y spheres were attached to the center cube to keep them together. They can't recieve On$$anonymous$$ouse functions as childs, as it seems.
Answer by Mmmpies · Feb 03, 2015 at 07:20 PM
I don't believe that's true. I created a new sphere/light and a new cube. I kept the scripts on the spheres (one on each) and made them children of the cube:
matched the light to the sphere script slot on both spheres and also added the OnMouseExit() to switch off the light.
Both lights work perfectly, I'm using unity free/indie and version 4.6.1f1.
EDIT
And Here is a link to a video showing it working
using UnityEngine;
using System.Collections;
public class ButtonCheck : MonoBehaviour {
public Light BtnLight;
void Start () {
BtnLight.enabled=false;
}
void OnMouseOver () {
BtnLight.enabled=true;
}
void OnMouseExit() {
BtnLight.enabled=false;
}
}
Answer by skunity3D · Feb 04, 2015 at 01:18 PM
Hello Mmmpies,
I would like you to see that its not about the nesting of gameobjects, its about the colliders. I have used your script "ButtonCheck" in the following image.
There are two setups :
Setup 1. Cube with two child spheres and the cube has a collider which has its box collider size values set to 1, 1, 1. These are the ones in row 1. So in this set up when you rollover onto the sphere the light shows up as intended, and you can see it in first two images in the collage above.
Setup 2. Cube with two child sphers but here this cube has a collider which has it box collider size values set to 10, 2, 5. These are the ones in row 2. But now when you rollover to two spheres below nothing happens as you can see in the third (image top right) in the collage above.
The reason is when see the fourth image in bottom left of the collage, you would realize that the cube's box collider overlaps the its child spheres hence their sphere colliders are encapsulated as well. Thus the OnMouseOver script on the spheres wont work. But if you disable the Box collider and then check, OnMouseOver script will start working as intended on the spheres in setup2.
So UniqueMAX, the spheres being child of cube is not the problem, just check whether their (sphere's) collider are not being obstructed by any other collider towards the camera.
Regards,
To be fair @skunity3D I really didn't think it was about nesting, that's the exact point I was making to @Unique$$anonymous$$AX, and you're right it could be an extended collider on the parent but it wouldn't have to be a parent.
Any collider from any object that extends over the collider on the sphere would cause the issue. You can test it yourself, just create cube and a sphere objecte and put an On$$anonymous$$ouseOver script on the sphere to Debug.Log out a message and extend the cubes box collider until it covers half the sphere.
you'll see, even if not a parent, the collider will block the half of the sphere that it covers.
Your answer
Follow this Question
Related Questions
Inconsistent Input.GetMouseButtonDown Detection 0 Answers
when mouse pass over a button,the console print some content 1 Answer
Change GUI on mouse over 1 Answer
Getting mouse over GUI event 0 Answers
Mouse Over Inconsistency 0 Answers