0
\$\begingroup\$

I have a simple object:

Object

I want to create a highlight effect.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Map : MonoBehaviour
{
    public Camera mapCamera;
    public Text objectWasHit;

    private Camera[] cameras;
    private GameObject mouseOvered;

    // Use this for initialization
    void Start()
    {
        cameras = Camera.allCameras;

        GameObject levels = GameObject.Find("_Level");
        foreach (Transform child in levels.transform)
        {
            child.gameObject.AddComponent<Renderer>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapCamera.enabled == false)
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = false;
                }
                mapCamera.enabled = true;
            }
            else
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = true;
                }
                mapCamera.enabled = false;
            }
        }

        bool rcHit = false;
        Vector3 mouse = Input.mousePosition;
        Ray castPoint = mapCamera.ScreenPointToRay(mouse);
        RaycastHit hit;
        Debug.DrawRay(castPoint.origin, castPoint.direction * 1000, Color.magenta);
        if (Physics.Raycast(castPoint, out hit, 1000))
        {
            rcHit = true;
            if (mouseOvered != hit.collider.gameObject)
            {
                mouseOvered = hit.collider.gameObject;
            }
            objectWasHit.text = mouseOvered.name;
        }

        if (!rcHit && mouseOvered != null)
        {
            //do your thing to undo the material change
            mouseOvered = null;
        }
    }
}

First I'm not sure if white color is the right way for highlighting. Second there is no startcolor since the object don't have any renderer or material at the first time. Then how can I make the highlight effect ?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can't render anything without those components. So you're selecting the wrong object. White gives the original color btw sample * 1.0 = sample \$\endgroup\$
    – Sidar
    Jan 19, 2018 at 21:27

1 Answer 1

3
\$\begingroup\$

When the object would have no Renderer, then it would not have an appearance in the game, so it would not make sense to highlight it.

However, it might be possible that the renderer which visualizes the object is not on the object itself but in one of its child-objects. To get the first renderer in the children of the object, use GetComponentInChildren<Renderer>(). This, however, might give you the wrong one if there are multiple children with renderers.

If the object has multiple sub-objects with their own renderers, you might want to use GetComponentsInChildren<Renderer>() (plural). This will give you an array with all the renderers of all the children of the game object.

Now regarding trying to highlight something with renderer.material.color = Color.white: Pure white is the default color of most renderers. So unless the renderer's color was manually changed to something else, this line won't have any visible effect except removing a different coloration you set before.

The simple solution to just see something for now and prove that your code works, would be to use a different color like Color.red. You can also create a custom color which is "whiter than white" to render the object brighter than usual:

private static Color OVERBRIGHT = new Color (2.0f, 2.0f, 2.0f);

You could also look at the material of the object and see if its shader has any additional color properties which might give you the effect you are looking for. The Standard shader for 3d meshes, for example, has the Emissive color property which causes the object to get illuminated in addition to its illumination from light sources.

renderer.material.SetColor("_EmissiveColor", Color.white);

(note: the "Emission" checkmark in the material properties needs to be set in order for "_EmissiveColor" to have any effect)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .