Search Unity

OnDrawGizmos not being called

Discussion in 'Scripting' started by Xenon42, Nov 14, 2018.

  1. Xenon42

    Xenon42

    Joined:
    Aug 31, 2016
    Posts:
    15
    I was trying to make some simple scripts to test stuff out, but for some reason OnDrawGizmos() is never being called. I've even created a basic script, that contains nothing but OnDrawGizmos(), and nothing is happening:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class GizmoTest : MonoBehaviour
    5. {
    6.      void OnDrawGizmos()
    7.      {
    8.          print("Drawing Gizmos!");
    9.          Gizmos.color = Color.white;
    10.          Gizmos.DrawSphere(Vector3.zero, 1f);
    11.      }
    12. }
    If I understand Unity scripting right, this should create a white sphere, one unit in radius, at the zero point of the scene, even when in edit mode. Further, it should print "Drawing Gizmos!" to the console constantly, so I can see it's functioning. But the script does nothing, in both edit mode AND play mode. No sphere is visible, nothing is output to the console.

    Is there some visibility setting for Gizmos, that I'm unaware of? That's the only explanation I can think of for why this script isn't working. I even tried upgrading to the latest Unity build (2018.2.15f1 at the time of writing) and the script still does not work.
     
    deplorablemountaineer likes this.
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Works fine for me.
    I gues you haven't put it on a scene object
     
    Bunny83 likes this.
  3. Xenon42

    Xenon42

    Joined:
    Aug 31, 2016
    Posts:
    15
    It is on a scene object, though.

    Still nothing is happening.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    For some reason when I had that issue, adding a Start() method did the trick. You'll see the difference when you get the Checkbox to disable the script. Strange, but that's how Unity seems to roll.

    -ch
     
    Last edited: Nov 15, 2018
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Nah, doesn't need the checkbox to work.
    There's clearly something else going on
     
  6. Xenon42

    Xenon42

    Joined:
    Aug 31, 2016
    Posts:
    15
    Tried this. Still doesn't work.
     
  7. Xenon42

    Xenon42

    Joined:
    Aug 31, 2016
    Posts:
    15
    Okay, so I have been looking at some other elements of my project, and I believe I've found the problem.

    I was looking through the Scene View Gizmos drop-down, and couldn't find a "toggle all" button. But I did see the option for the Camera Gizmo, among others, and remembered that Unity's base components use lots of of Gizmos themselves. I noticed then that neither the Camera nor the Directional Light in my scene were displaying any Gizmos, nor did they have any visible icons to show their positions in 3D space. Which, okay, if OnDrawGizmos() isn't being called for my scripts, why would it be any different for Unity's default components?

    But what seemed strange to me, was that earlier today I was fiddling with a different project I created some time (and several Unity updates) ago, and all the Gizmos in that project were working fine. I even copied over my GizmoTest script and it worked fine in that older project. It worked even after I loaded the older project into the newest Unity version.

    So... I just created a new project in the latest Unity version (2018.2.16f1 at the time of writing) and now all my Gizmos work perfectly. I guess there was some kind of bug with creating new projects in one of the previous versions of Unity? Unfortunately, I'm not sure which version that was, other than it being earlier than 2018.2.15f1. It seems to be fixed now, in any case.

    Thank you all for your help, regardless.
     
  8. Casiell

    Casiell

    Joined:
    Jul 22, 2016
    Posts:
    7
    Ok, I recently had the exact same problem and the solution for was folding out the script in the inspector with that little arrow next to component icon. It's the same case as with colliders, when they are folded, gizmos are not drawn
     
  9. Noxury

    Noxury

    Joined:
    Mar 8, 2015
    Posts:
    22
    Common Mistakes
    1. Make sure the Component of the script is expanded in Inspector
    2. Enable Gizmos at the Scene View Window
    3. Check if Gizmo for particular Component is enabled (expand Gizmos-Button at Scene View Window)
    4. Scene-deactivated Layers (Top Right enum "Layers") will override everything so no gizmos are seen in Sceneview even when 1 - 3 are done. Tick the corresponding closed eye to be able to see the gamobjects and their containing Gizmos again.
     
    Last edited: Apr 13, 2020
    Bunny83, VinyxAi, zelderus and 4 others like this.
  10. Robert-Brain

    Robert-Brain

    Joined:
    Nov 11, 2015
    Posts:
    1
    upload_2019-7-17_16-33-5.png
    Try toggle this buttons on, just like this:
    upload_2019-7-17_16-33-35.png
     
    shiki_2022, Alekxss, Bunny83 and 9 others like this.
  11. ajeet00552066

    ajeet00552066

    Joined:
    Apr 20, 2018
    Posts:
    1
  12. SepraB

    SepraB

    Joined:
    Sep 14, 2016
    Posts:
    6
    You have to make sure the component (script) is expanded in the inspector.
     
  13. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    i found the solution...check z depth of object
     
  14. stefanvielfrass1404

    stefanvielfrass1404

    Joined:
    May 10, 2020
    Posts:
    1
    Reset the transform component. As example when you say to draw a Sphere at position 0, 1 then it doesn't matter if you move your plane or object to another place it still draws the spheres at the world position 0,1
     
    Filip8429 and lucassivolella like this.
  15. guodan

    guodan

    Joined:
    Dec 22, 2016
    Posts:
    29
  16. nights007

    nights007

    Joined:
    Aug 12, 2014
    Posts:
    17
    Excellent, had to toggle gizmo button
     
  17. turick00

    turick00

    Joined:
    Jul 14, 2020
    Posts:
    31
    Did all these steps, nothing worked. My solution was, I needed to add an event system to the scene.
     
  18. WARW0LF

    WARW0LF

    Joined:
    Oct 1, 2016
    Posts:
    16
    gizmos won't take in a color set in the inspector using public Color, you have to set the gizmos color before its draw with Gizmos.color = Color.red or whatever color, this was my issue
     
    Mizuiky and mushtaqhussainrezaie like this.
  19. lucassivolella

    lucassivolella

    Joined:
    Jul 14, 2020
    Posts:
    6
    Thanks man, I spent around three hours trying to figure it out.
     
  20. Shiverish

    Shiverish

    Joined:
    Jan 14, 2014
    Posts:
    2
    Close to being a hero, thanks.
     
  21. deplorablemountaineer

    deplorablemountaineer

    Joined:
    Jun 16, 2018
    Posts:
    11
    Mine too, but the real problem is, by default, alpha is zero in inspector-set colors. You have to make sure alpha is a full 100% or the gizmos are invisible!
     
    Browdy likes this.
  22. EsmailKM

    EsmailKM

    Joined:
    Dec 27, 2019
    Posts:
    2
    sometimes you need to zoom in the inspector to see that.
     
  23. ZoeWheat

    ZoeWheat

    Joined:
    Nov 9, 2020
    Posts:
    2
    Savior!
     
  24. virtueone

    virtueone

    Joined:
    Jun 15, 2015
    Posts:
    12
    This problem can also occur if you have two inspector windows open and one of them is locked.
     
    the_real_ijed likes this.
  25. andradevaldemar298

    andradevaldemar298

    Joined:
    Jun 21, 2020
    Posts:
    1
    My solution was to pass float value in method Gizmos.DrawWireSphere(transform.position, 5f);
    I was passing an int variable instead of float
     
  26. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I worked through all of those only to find out I had forgotten an "s" in the functions name:
    OnDrawGizmo should be
    OnDrawGizmos

    ;)
     
  27. DCTechnologies

    DCTechnologies

    Joined:
    Jul 26, 2019
    Posts:
    1
    Make sure you have the game object with the script for drawing the gizmos selected. That's what worked for me.
     
  28. goncalomiguelleitao

    goncalomiguelleitao

    Joined:
    Jul 19, 2021
    Posts:
    1
    I was having exactly the same problem has him. The little checkbox for the script on the inspector window, didnt appear to me either. My problem was that the alpha of the color i was using was 0, so the gizmo was trasparent.
     
  29. mrstruijk

    mrstruijk

    Joined:
    Jan 24, 2018
    Posts:
    52
    This was it for me too!

    One easy solution is to set the default Color in code:
    [SerializeField] private Color m_color = Color.blue;

    This automatically sets an Alpha of 1
     
    Last edited: Jun 10, 2022
    lolopupka likes this.
  30. Nono_fr

    Nono_fr

    Joined:
    Jan 2, 2022
    Posts:
    1
    After hours of debuging and searching with nothing happen while the function is correctly called, and the color properly set. That was it. The alpha value of the color ....
     
  31. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Neither of my functions (OnDrawGizmos, OnDrawGizmosSelected) were being called and I did everything.
    Turns out the layout of my Unity editor got "stale", and there's no real way to check other than go back to the Default (or your own Layout in the top-right of Unity), and make your windows nice as you want them again.

    upload_2023-7-26_15-39-47.png

    After resetting my layout, it worked and started getting called.
    Go figure.
     
    NVriezen and gamedevjake like this.
  32. FelipeCavaco

    FelipeCavaco

    Joined:
    Sep 22, 2021
    Posts:
    3
    Hi, just giving my 2 cents here in case someone else have the same problem as me: make sure the object is also visible by disabling the eye icon in the hierarchy. If the object is invisble, the Gizmos methods won't be called either, even if the object itself is enabled!

    upload_2023-11-24_16-37-15.png
     
  33. gamedevjake

    gamedevjake

    Joined:
    Aug 16, 2022
    Posts:
    26
    Resetting my layout worked as well. Annoying, but at least the gizmo shows up now. Thank you!
     
    NVriezen likes this.
  34. H4CK3RD4WG

    H4CK3RD4WG

    Joined:
    Mar 31, 2015
    Posts:
    2
    +1 to resetting layouts to get Gizmos to render
     
    NVriezen likes this.