- Home /
OnMouseEnter is Inconsistent
My problem relates in many ways to this problem: http://answers.unity3d.com/questions/350679/inconsistent-onmouseover-caused-by-instantiate.html
The problem is that I have an ordenary Capsule, with a script like this:
using UnityEngine;
using System.Collections;
public class Enemymenu : MonoBehaviour
{
private EnemyBasicBehavior enemy;
private bool mouseOver = false;
// Use this for initialization
void Start()
{
enemy = this.GetComponent<EnemyBasicBehavior>();
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (mouseOver)
{
string stats = "";
stats += "Health: " + enemy.getHealth() + "\n";
stats += "ATCD: " + enemy.getAttackCountDown() + "\n";
GUI.Box(new Rect(Screen.width - 100, 0, 100, 50), stats);
}
}
void OnMouseEnter()
{
mouseOver = true;
}
void OnMouseExit()
{
mouseOver = false;
}
}
But when I create a couple of instanses of my capsule it works very inconsistent. On one object it work fine, on others it only works if I do it from a special angle and others again does not work at all.
I create the instances out from a Prefab, so there is no difference in setup.
My script, Collider and Mesh is on the same object, so there is no problems with children object.
I tried to make the collider big enough to consume the whole object and trying to remove it, without any effect.
There is no errors or warnings in the log.
I tried googling this problem without finding anyone asking the same question.
kind regards.
Answer by alexpizzini · Dec 14, 2012 at 10:38 PM
I'd start with a Debug.Log("mouse enter"); in your OnMouseEnter() method. See if its a problem with detecting when the mouse is over the game object or something else. When you instantiate it, are you setting the position and scale?
What do you mean by special angle? Is this something like a FPS, and you're checking stats on enemies you look at? If you pause the game in the editor, you should be able to click on these enemies and check their colliders.
"I'd start with a Debug.Log("mouse enter"); in your On$$anonymous$$ouseEnter() method." Don't change the result, it shows the same inconsistencies.
"When you instantiate it, are you setting the position and scale?"
I have a box with a boxcollider that is a spawn area, and I create the enemies with this code:
"Bounds bounds = gameObject.GetComponent().bounds;
GameObject clone = (GameObject)GameObject.Instantiate(enemy, new Vector3(Random.Range(bounds.$$anonymous$$.x, bounds.max.x), Random.Range(bounds.$$anonymous$$.y, bounds.max.y), Random.Range(bounds.$$anonymous$$.z, bounds.max.z)), transform.rotation);"
And that works with the same inconsistent result. But I also tried to create the Enemies manual, by dropping the prefabs into the scene, with the same inconsistent result.
"What do you mean by special angle?"
I have a third person controller that i can move around in a hack'n'slash kind of setting. So when I look from one angle then it works on some object, but from others it does not.
"you should be able to click on these enemies and check their colliders." I wrote that i tried to make the colliders very big and trying to remove them. $$anonymous$$aking them big made no change, while removing them made the code stop working completely as expected.
Every time I play the scene the different objects react the same way. So the inconsistency does not change from play through to play through, so there is definitely something I am missing.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Turning off renderer in other game object (C#) 2 Answers
Instantiate Object as GameObject 3 Answers
Want to move 2d fish sprites in pond ! 0 Answers