Highlighting Game Objects in game

So I am beginning to dive deeper into my first application.
I have a series of GameObjects that get created/instanced with new information attached to them. I would like to add some interactivity to them. Here’s what I’m wanting to do:

1: When I hover my mouse over one of the objects, I’d like it to be bracketed . I’d like it to always be facing the camera regardless of where the camera (billboard?) gets positioned/rotated in the scene. So my question is how would I do this? Would I do this with UI elements, or some other element?
2: When I click it, the bracket changes from to a circle that behaves the same way (custom texture/material)

Any help to point me in the right direction would be appreciated.

Why not a bounding box : https://www.assetstore.unity3d.com/en/#!/content/10962

You can use LookAt to face the camera.

  • Sound boarding…*
    I don’t think this will work. I’m trying to do something with custom textures. The LookAt option could force a 2D texture to Billboard to the camera perfectly.

The scaling I guess could be connected to the physical distance from the object .No real work needs to be done there which is a plus.

I think this might work. Thoughts?

Toucan use a physics raycaster with the pointer enter and exit triggers on your GameObject to detect the hover.

No raycast necessary. OnMouseEnter and OnMouseExit in a script will do it to detect a mouse hover.

The physics raycaster with the event system plays nicer with the new UI system then OnMouseDown. Only applicable if you have 4.6 or above.

Ok. So I have finally started on the part of the project and I’m already running into a problem.

I was thinking that I could use a plane, but it doesn’t render (no texture)
I thought about using a Sprite.

Also, I am attempting to assign images in the variable but I’m getting errors. I’m assuming Image is not a valid type. Should I be using Texture instead?

What are your thoughts on using a plane vs using a Sprite?

I discovered the quad object… this makes alot more sense to use instead of the plane…
I still am not sure how to get the main texture to change based on onHover, offHover, and click/active states.

After some searching, I found this bit of code:

renderer.material.mainTexture = Texture;

Let the testing commence.

More questions…

So I see i can find the number of children a transform has (Transform.childCount). Is there a way to list the children?

Perhaps combining childCount and getChild?

Transform is enumerable. That means you can do this

foreach (Transform child in transform){
    // Do something suitably awesome with each child
    Debug.Log(child.name);
}

Mr Terminal, use sprites!

Check your images in the project library and you can set them to sprites instead of textures. Sprites can be placed in the Unity scene as is, without needing to apply them to any objects/geometry. They are specifically designed for putting flat/billboard images in Unity, are very well handled in code, and have more features than regular textures do!

Edit: You can also make a sprite, child it to the normal sprite, and make it appear or dissapar when the mouse is hovered over. Nice and simple.

If you put public Sprite thingie; in a script, then you can drag other sprites into it via the inspector and do the things such as making them visible, etc.

Edit 2: You can also use the free iTween to make cool bounce tweens or whatever when the sprite is clicked,

I got it mostly working with the Quad. I’ll test out the Sprite (now that I have some of the functionality working)

Is there a way to check if I clicked outside the colider? that way I can turn a state off?

Last question of the night. I am attempting to use the for each loop to unhide/enable child transforms. I’ve tried a couple of different things but they do not behave as I wanted them to. (Specifically SetActive() )

Think I should use raycasts to verify if I clicked on the object/transform? or is there a way that shows more finesse ?

so I did some research and some testing and I came across hit.transform. I modified the example given in the documentation to do my tests, but the outcome is not what I expected. If I click on the object that has this script, it returns the transform. Perfect. Expected that.

However, if I click outside the object nothing happens. Why is that? The logic here makes me think that if nothing is returned (null value), it should print “Missed” to the console. Obviously I messed up my logic somewhere, or perhaps I’m testing for the wrong thing?

function Update ()
{
    if (Input.GetMouseButtonDown(0))
    {
        // Check to see if the object was clicked
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, hit))
        {
            if(hit.transform !=null)
            {
                print(hit.transform);
                //selected=false;
                //iconQuad.renderer.material.mainTexture = offImage;
                //renderer.material.color = offColor;
            }
            else if(hit.transform == null)
            {
                print("Missed");
            }
        }
    }
}

Untested, but try this

function Update ()
{
   if (Input.GetMouseButtonDown(0))
   {
      // Check to see if the object was clicked
       var hit: RaycastHit;
      var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       if (Physics.Raycast(ray, hit))
       {
           if(hit.transform !=null)
           {
              print(hit.transform);
              //selected=false;
               //iconQuad.renderer.material.mainTexture = offImage;
              //renderer.material.color = offColor;
           }

      }
       else
       {
                print("Missed");
       }

   }

No change.

It isn’t really this hard to determine if I clicked the object the script is attached to is it?

Am I perhaps going about this the wrong way?