some 3d objects not responding to onMouseEnter and onMouseDown
I have a map made out of 3d objects and every one of them have the Node.cs script, in Node.cs the node changes color when onMouse enter and changes back when mouse Exit, for most of the nodes it works, but for the nodes at the top of the screen(about 20) it would not change color or Debug.Log() anything. There are a total of about 250 nodes in the screen and its all inside a GameObjects called Nodes witch has no collider or Rigidbody.
This is Node.cs:
using UnityEngine;
using UnityEngine.EventSystems;
public class Node : MonoBehaviour {
public Color hoverColor;
public GameObject Turrent;
private Renderer rend;
// Use this for initialization
public Vector3 positionOffset;
private Color originalColor;
public Color ErrorColor;
private void Start()
{
rend = GetComponent<Renderer>();
originalColor = rend.material.color;
}
public Vector3 GetBuildPosition()
{
return transform.position + positionOffset;
}
private void OnMouseDown()
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
if (!BuildManager.instace.canBuild)
{
return;
}
if (Turrent != null)
{
return;
}
BuildManager.instace.BuildTurrentOn(this);
}
void OnMouseEnter()
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
if (!BuildManager.instace.canBuild)
{
return;
}
if (!BuildManager.instace.EnoughMoney)
{
rend.material.color = ErrorColor;
return;
}
rend.material.color = hoverColor;
}
void OnMouseExit ()
{
rend.material.color = originalColor;
}
}
Are the objects being instantiated with code or are they setup manually in the editor? If you are setting them by hand be sure they are all from the same prefab. Also you may find that your draw calls are growing fast as I'm pretty sure you make a new instance material for each object when you call rend.material.color the first time you call it. I don't think that would contribute to your problem, but it might be better to create a material for each color ins$$anonymous$$d and swap them when you need to change the look. The only other thing I can say and I really don't know because I cannot see the setup you have for the level, is if the camera frustum and the objects bounds are messing up their rendering some how.
Answer by DawdleDev · Sep 03, 2018 at 08:27 PM
If you're going to use OnMouseEnter and Exit, you're going to need something to call it. Either you can use a bunch of static colliders, or you can use an EventTrigger component, which may be more performant and doesn't have to use physics.