Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How make the timer active when the object in the camera shot

Discussion in 'Scripting' started by rhendra, Sep 20, 2018.

  1. rhendra

    rhendra

    Joined:
    Dec 27, 2017
    Posts:
    44
    #ASK help me guys.
    how to make my timer active when the object in the camera shot?

    here my script:


    public float waitTime = 1f;
    public int dollar;
    public bool timerRunning;

    void Start () {

    }

    void Update ()
    {
    if (timerRunning)
    {
    Oncom();
    }
    }
    void Oncom()
    {

    dollar--;

    if (dollar > waitTime) {

    timerRunning = true;
    }
    else if ( dollar < 0)
    {
    dollar = 0;
    }
    }
    }
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
  3. rhendra

    rhendra

    Joined:
    Dec 27, 2017
    Posts:
    44
    it cannot work

    how to implement that


    public int dollar;
    public bool timerRunning;


    public SpriteRenderer rend;


    void Start () {

    rend = GetComponent<SpriteRenderer>();
    }

    void Update ()
    {
    if (timerRunning)
    {
    OnWillRenderObject();
    }
    }

    void OnWillRenderObject()
    {
    dollar--;

    if (dollar < 1) {
    dollar = 0;
    timerRunning = true;
    }
    else if ( dollar < 0)
    {
    dollar = 0;
    }
    }
    }
     
  4. rhendra

    rhendra

    Joined:
    Dec 27, 2017
    Posts:
    44
    they time still running when out of the camera
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    You don't need to call 'OnWillRenderObject' yourself (from the Update function) - Unity will call it itself when the object is visible.