Kien Mai
Kien Mai

Reputation: 11

Why is my method called by Unity when I don't call it in code?

I can not understand why the method OnDrawGizmos() executes while it is not called in both the Start() method and Update() method, but it is executed when I run the Unity project.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public Transform grounder;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnDrawGizmos(){
        Gizmos.color = Color.white;
        Gizmos.DrawSphere (grounder.position, 1);
    }
}

Upvotes: 1

Views: 1101

Answers (2)

SBI
SBI

Reputation: 2322

As the name suggests, OnDrawGizmozs gets called by the unity engine. It's a message function that gets called on classes that implement MonoBehaviour. Excert from the documentation:

Description

Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html

This means you don't need to call the function yourself.

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151604

Because it's called by Unity on classes that implement MonoBehaviour, according to the documentation.

Upvotes: 2

Related Questions