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. Dismiss Notice

Cant reference a float value

Discussion in 'Scripting' started by Abrakidabra, May 13, 2017.

  1. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62
    Im trying to make a highscore board for the fastest laps.



    I have a float called "t" in another script which is current time - start time

    I have a line of code which says " when we start a new lap, make the start time = current time"

    hence t is basically my laptime

    I want to

    1. Find t (in timer script)

    2. Run my if statement when my lap resets

    2. if t is less than my current high score (tHighscore) then

    3. set tHighscore = t

    4. update UI with string version of tHighscore


    Problem is i started coding a few days ago and I suck. Everything i have made is kind of just adapting things ive seen in tutorials and i dont know how to find t properly.


    I cant seem to find t.

    Error = Expression denotes a type, where a variable, value or method group was expected.

    Code for the checkpoint.cs script

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class checkpoint : MonoBehaviour {
    7.  
    8.     public GameObject TextTimer; //referemce to timer script
    9.  
    10.     public Text LapCounter; //reference to laps script
    11.  
    12.     public Text textHighscore; //reference to Highscore UI text
    13.  
    14.     public float tHighscore; // reference to current Highscore value
    15.  
    16.  
    17.  
    18.     void  Start ()
    19.     {
    20.  
    21.     }
    22.  
    23.     void  OnTriggerEnter ( Collider other ) //when we hit a checkpoint
    24.     {
    25.         //Is it the Player who enters the collider?
    26.         if (!other.CompareTag("Player")) return; //If it's not the player dont continue
    27.  
    28.  
    29.  
    30.         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
    31.         {
    32.             //Check so we dont exceed our checkpoint quantity
    33.             if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length)
    34.             {
    35.                 //Add to currentLap if currentCheckpoint is 0
    36.                 if(Laps.currentCheckpoint == 0)
    37.                     Laps.currentLap++;
    38.                 Laps.currentCheckpoint++;
    39.  
    40.            
    41.             }
    42.  
    43.  
    44.             if (Laps.currentCheckpoint == 1) // if we are passing the start/finish line
    45.            
    46.             {
    47.                 //save highscore
    48.  
    49.                 tHighscore = 9999f; // define current highscore value
    50.  
    51.                 if (TextTimer.GetComponent(Timer).t < tHighscore )
    52.                 {
    53.                     tHighscore = TextTimer.GetComponent(Timer).t;
    54.                 }
    55.  
    56.            
    57.  
    58.                 string hsminutes = ((int)tHighscore / 60).ToString (); // make thighscore into string
    59.                 string hsseconds = (tHighscore % 60).ToString ("f2");
    60.  
    61.                 textHighscore.text = hsminutes + ":" + hsseconds; //display string on UI
    62.  
    63.  
    64.                 //reset timer at the end of lap
    65.  
    66.                 TextTimer.GetComponent<Timer>().startTime = Time.time;
    67.  
    68.             }
    69.             else
    70.             {
    71.                 //If we dont have any Checkpoints left, go back to 0
    72.                 Laps.currentCheckpoint = 0;
    73.  
    74.  
    75.             }
    76.  
    77.  
    78.  
    79.             string CurrentLap = Laps.currentLap.ToString (); // Converting number of laps into string
    80.  
    81.             LapCounter.text = "Lap : " + CurrentLap; //lap counter UI
    82.  
    83.  
    84.  
    85.  
    86.         }
    87.  
    88.  
    89.     }
    90.  
    91. }
    92.  

    And the timer script with t defined in it

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Timer : MonoBehaviour {
    5.  
    6.     public Text timerText;
    7.  
    8.     public float startTime;
    9.  
    10.     public float t;
    11.  
    12.     public float tHighscore;
    13.  
    14.  
    15.  
    16.  
    17.     // Use this for initialization
    18.          void Start () {
    19.      
    20.         startTime = Time.time;
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.         void Update () {
    26.  
    27.           t = Time.time - startTime; //define t
    28.  
    29.  
    30.         string minutes = ((int)t / 60).ToString (); //make t into string
    31.         string seconds = (t % 60).ToString ("f2");
    32.  
    33.  
    34.  
    35.         timerText.text = minutes + ":" + seconds; // display string on UI
    36.  
    37.  
    38.      
    39.     }
    40.  
    41.  
    42.     }
    43.  
    44.    


    Any help appreciated thanks!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is the timer script on the TextTimer object?
    If so, I always use the generic version of GetComponent.
    Code (csharp):
    1.  
    2. if(TextTimer.GetComponent<Timer>().t < tHighScore) {
    3.    tHighscore = TextTimer.GetComponent<Timer>().t;
    4.    }
    5.  
    Note: The generic is not the only option, I just use it as a choice.
    You can refer to this page for examples of variations: https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    If I got the lines wrong about where/what the problem is, please explain. Say which lines (from your post) cause the problem.

    Also note, I would recommend that you delay updating the Timer Text , from Update's frequency to something slower, like every 1/2 second or second. 30-60+ times per second to update the timer seems a bit silly/overkill. :)
     
    Abrakidabra likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    +1 on the generic version. I think it's considered the best way of getting it(can't remember where I read this honestly). For that fact, the way GetComponent(timer) is written is probably why the error occurs. I don't think this is correct at all.
     
    Abrakidabra likes this.
  4. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62

    Thanks alot for the response... Yes that is where the error is in the code.

    I added the parathesis but i still get the exact same error in the same place happening. Another error is coming up now in the same place too "Timer is a type but a variable was expected"


    I think my mistake is bigger than just missing paranthesis.


    The timer text is recording the current lap time to the UI with 2 decimal places, so I think i need to have it updating pretty quickly.


    Edit : ignore me not reading your answer properly, it worked, thanks so much. Didnt realise i was actually that close to maiking it work
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Show us what you did because I think you still have it incorrect from the sounds of it. It should be angled brackets that are added around timer.
     
  6. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62

    Apologies, I mis copied your code and the game runs now, it actually works but it has a glitch


    i appears to be saving the lap to high score regardless of whether its the new fastest lap or not, IE just saving the last lap... must be a problem with "if(TextTimer.GetComponent<Timer>().t < tHighScore)" but cant see why
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's newer, from what I've read. I do kinda remember the other ways and how they work, but I don't devote a lot of "memory" (in my head) to how they are written lol. I can look it up, for a forum post, but I don't need to know them all when I write ;)
     
  8. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62
    Im also foreseeing a problem with the fact the the first time u cross the start/finish line off the grid, it moves the lap from 0 to 1, so it records that as a lap time.... is there anyway to exclude the first lap from effecting the highscores?

    haha I dont want you to have to write my whole game but ive seriously been stuck on this for the last 2 days and i just really want to not have to look at this fastest lap part again :oops:
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Because you keep setting the variable just above there, ie:
    Code (csharp):
    1.  
    2. tHighscore = 9999f; // define current highscore value
    3.  
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Try to work with the adjustments here and see what you come up with .. and if this page is bumped in the future by yourself or another poster (for updates and/or if you're still stuck), I'll look again :)
     
    Abrakidabra likes this.
  11. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62

    Ohhhh of course, the reason i done this is to make the default always slower than whatever the first lap is and clearly didnt think it through..

    is there a way to make it so the first lap always becomes thighscore and then subsequent laps are assesed against that lap?

    this is way harder than i thought it would be, spend 50% of development time on just this so far haha
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Of course, you could easily set that at the start of the script, instead of in the lap checking area ;)
     
    Abrakidabra likes this.
  13. Abrakidabra

    Abrakidabra

    Joined:
    May 12, 2017
    Posts:
    62
    Solved.


    Thanks so much to everyone who helped! Spend so much time starting at that code, sometimes you just need someone else to see the errors
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool...

    glad it's all fixed :)

    Enjoy your game.
     
    Abrakidabra likes this.