Search Unity

trying to implement my own Timer class - having problems

Discussion in 'Scripting' started by paco, Dec 22, 2010.

  1. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    hey guys,

    I'm trying to implement my own Timer-Display Class in JScript.
    (Unfortunately without success)

    The Timer-Code itself I've got from this page:
    http://www.der-softwareentwickler-blog.de/2010/08/02/unity-3d-tutorial-22-countdown-mit-yield-waitforseconds/
    (and from another page, but can't find the link right now..)

    The Problems I've encountered:
    Well, my Console-Debug-Output never happens.
    (I'm using yield for the first time btw. I'm sure I'm doing something wrong with this,
    but I've allready tried so many different things. ^^)

    Here's the code I've got so far:

    PHP:

    class TimerDisplay {
        
        private var 
    seconds int;
        private var 
    timeStart;
        private var 
    time;
        private var 
    restSeconds int;
        private var 
    roundedRestSeconds int;
        private var 
    displaySeconds int;
        private var 
    displayMinutes int;
        
    //private var guiCountDown : GUIText;
        
        
    function Initiateseconds ) {
            
    this.seconds seconds;
        }
        
        function 
    myFunc() {
            
    Debug.Log"test1" );
            var 
    myVar doIt();
        }
        
        function 
    doIt() : IEnumerator {
            
            
    Debug.Log"test2" );
            
    this.timeStart Time.time;
            
            for( var 
    this.seconds0i-- ) {
                
                
    this.time               Time.time timeStart;
                
    this.restSeconds        this.seconds - ( this.time );
                
    this.roundedRestSeconds Mathf.CeilToInt(restSeconds);
                
    this.displaySeconds     roundedRestSeconds 60;
                
    this.displayMinutes     roundedRestSeconds 60
                
                
    text String.Format "{0:00}:{1:00}"displayMinutesdisplaySeconds ); 
                
    //this.object.GUI.Label( Rect ( 400, 25, 100, 30 ), text );
                
    Debug.LogdisplayMinutes " " displaySeconds );
                
                yield 
    WaitForSeconds (1);
            }
        }
    }

    The Method-Function MyFunc() was just one of my many tests.
    (I read that yield does a return when called, that's why i tried that..)

    What am I doing wrong?
    Please help! :)
    Thanks
     
  2. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    hey,

    I made some progress.
    My Class works, if it inherits from MonoBehaviour!
    (I'm guessing YIELD only works when inheriting from Unity's GameObject or MonoBehaviour Class)

    There is still one little question I have!

    First, here's the code I'm using at the moment:

    TimerDisplay.js
    PHP:

    class TimerDisplay extends MonoBehaviour {
        
        private var 
    seconds int;
        private var 
    timeStart;
        private var 
    time;
        private var 
    restSeconds int;
        private var 
    roundedRestSeconds int;
        private var 
    displaySeconds int;
        private var 
    displayMinutes int;
        
        function 
    Initseconds ) {
            
    this.seconds seconds;
        }
        
        function 
    doIt() {
            
            
    this.timeStart Time.time;
            for( var 
    this.seconds0i-- ) {
                
                
    this.time               Time.time timeStart;
                
    this.restSeconds        this.seconds - ( this.time );
                
    this.roundedRestSeconds Mathf.CeilToInt(restSeconds);
                
    this.displaySeconds     roundedRestSeconds 60;
                
    this.displayMinutes     roundedRestSeconds 60
                
                
    Debug.LogdisplayMinutes " " displaySeconds );
                
    text String.Format "{0:00}:{1:00}"displayMinutesdisplaySeconds ); 
                
    //GUI.Label( Rect ( 400, 25, 100, 30 ), text );
                
                
    yield WaitForSeconds (1);
            }
        }
    }

    onStart.js
    PHP:

    function Start () {
        
    Debug.Log"Start" );
        var 
    timerDisplay gameObject.AddComponent("TimerDisplay");
        
    timerDisplay.Init);
        
    timerDisplay.doIt();
        
    Debug.Log"End" );
    }

    As you can see above, the Script is added in runtime with AddComponent.
    (the script-class is not attached to a gameobject)

    Unfortunately I can't access GUI.Label this way.
    I allways get the error message:
    "object reference is not set to an instance of an object"

    My Question:

    Is there any other way to make this work?
    To call GUI.Label from my Class without having to attach the Script to a GameObject?
    I thougt by calling AddComponent I would automatically attach the Script to the GameObject??

    greetz