Gizmo Draw

Hi :smile:
I used this code to draw my custom icon in editor as gizmo icon for my timer game object:
Original Code:

// Draws the Light bulb icon at position of the object.
// Because we draw it inside OnDrawGizmos the icon is also pickable 
// in the scene view.

function OnDrawGizmos () {
    Gizmos.DrawIcon (transform.position, "Timer.png", true);
}

I need make icon file name “Timer.png” as variable I tried make next change but console gave me an error
Original Code with some Changes:

// Draws the Light bulb icon at position of the object.
// Because we draw it inside OnDrawGizmos the icon is also pickable 
// in the scene view.

var Gizmo : GameObject;

function OnDrawGizmos () {
    Gizmos.DrawIcon (transform.position, Gizmo , true);
}

Console Report:

No appropriate version of 'UnityEngine.Gizmos.DrawIcon' for the argument list '(UnityEngine.Vector3, UnityEngine.GameObject, boolean)' was found.

What is the error message meaning ?

You’re trying to pass a GameObject in the second argument of DrawIcon(). The function clearly requires a String for that argument.

Gizmos.DrawIcon() Documentation:

static function DrawIcon (center : Vector3, name : String, allowScaling : boolean = true) : void

This my new code but no gizmos drawn !!!

var Gizmo : Texture2D;             // place here gizmo texture "Timer.png"
var GizmoString;		 	         // variable to save texture name as string
function Update()
{
GizmoString = Gizmo.name.ToString; // convert texture name to string
}
function OnDrawGizmos ()		        // draw gizmo function 
{
    Gizmos.DrawIcon (transform.position, GizmoString, true);
}

Console Error

InvalidCastException: Cannot cast from source type to destination type.
Timer_Gizmo.OnDrawGizmos () (at Assets/Circuspheres/Timer/Scripts/Timer_Gizmo.js:19)

No Solve …

You need to have a folder called Gizmos in the root of your project.
Read the Documentation. “The image file should be placed in the Assets/Gizmos folder.”
Your timer.png should be in there. Gizmos.DrawIcon unfortunatly looks only into that folder.

Lovely of you to help someone with a six year old problem.

1 Like

Bonus points for helping with UnityScript code.

Although the advice is kinda useful, for the future generations.