#Lemon Score Manager Thread

1 messages · Page 1 of 1 (latest)

vital fog
#

Start here

lunar pilot
#

@silk rock

#

damn all of this stemming from a score manager LOL

silk rock
#

im not a genius like all of you guys so yes

lunar pilot
#

you dont have to be to understand what digis saying lol

glass hollow
lunar pilot
#

hes breaking it down for a beginner

glass hollow
#

By asking you why you have that variable, my intent was to have you look at your code and consider the reasoning behind it being there and wonder if it had to be there at all. I see now this sort of self-reflection is beyond you

#

So I am now telling you to delete it

silk rock
#

ok ima do it

lunar pilot
#

throw in your updated code here too once you finish

glass hollow
#

Yes. There will be errors at first because we're not done yet

#

But share them anyway

silk rock
#
{
    public Score script;
    public float moneypersecond;
    public float Moneyperupgrades;

    void Update()
    {
        += moneypersecond * Time.deltaTime;
       int integerPart = Mathf.FloorToInt(=);

       // add the integer piece to our score.
       script.score += integerPart;
       // we have consumed that amount from this script
        -= integerPart;
    }

    public void Addscore()
    {
       if (script.score >= 100)
        {
            script.score -= 100;
            moneypersecond += Moneyperupgrades;
        }
    }

    
}``` deleted it
glass hollow
#

Now finish deleting it did you literally just leave the lines there without the variable in them

silk rock
#

yes

#

what var am i suposed to put there

vital fog
#

delete all of it

glass hollow
silk rock
#

oh ok

#
public class scorepersec : MonoBehaviour
{
    public Score script;
    public float moneypersecond;
    public float Moneyperupgrades;

    void Update()
    {
     
    }

    public void Addscore()
    {
       if (script.score >= 100)
        {
            script.score -= 100;
            moneypersecond += Moneyperupgrades;
        }
    }

    
}```done
glass hollow
#

Okay. Now, show Score

#

The script

silk rock
#
{
    public int score;
    public Text scoretext;
    public Button Upgrade1;
    public int Upgradeperclick = 0;
    
    
    // Start is called before the first frame update
    void Start()
    {
       Button upbtn = Upgrade1.GetComponent<Button>();
            upbtn.onClick.AddListener(TaskOnClick); 
    }

    // Update is called once per frame
    void Update()
    {
      scoretext.text = score.ToString();  
    }

    public void Addscore()
    {
        score += 1 + Upgradeperclick;
    }

    void TaskOnClick()
    {
        if (score >= 100)
        {
            score = score - 100;
            Upgradeperclick += 1;
        }
    }

}```
glass hollow
#

Okay, first question: Why are you referencing Upgrade1 here

#

What is the purpose of setting the on click here

silk rock
glass hollow
#

Okay so remove it and everything involved with it

silk rock
#
{
    public int score;
    public Text scoretext;
    public int Upgradeperclick = 0;
    
    
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
      scoretext.text = score.ToString();  
    }

    public void Addscore()
    {
        score += 1 + Upgradeperclick;
    }

    void TaskOnClick()
    {
        if (score >= 100)
        {
            score = score - 100;
            Upgradeperclick += 1;
        }
    }

}```
glass hollow
#

Okay now what are Addscore and TaskOnClick doing

#

And also Upgradeperclick

silk rock
#

ima reame upgradeperclikc

vital fog
#

Thats not what was asked

silk rock
#

its the amount of score you get when you click the button

glass hollow
glass hollow
#

This isn't a button

#

Why are we talking about buttons

#

this is your score manager script

silk rock
#

the main button

#

the addscore is the function it executes on click

glass hollow
#

Shouldn't be on this script

#

So that'll be a third script to make down the line but it shouldn't be on this one

#

This script is solely responsible for tracking score and displaying it

silk rock
#

okay

glass hollow
#

Show it when it's done

silk rock
#
{
    public int score;
    public Text scoretext;
    
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
      scoretext.text = score.ToString();  
    }

}```
glass hollow
#

Good. Now that all of the pointless things have been removed from both scripts, we can truly begin. First, that score, since it's ticked up per second and you want to have fractional points, should be a float

silk rock
#

done

glass hollow
#

Okay, now, let's go back to the scorepersec. First off, let's rename the variables into things that aren't garbage. Also making moneypersecond private since you won't need to modify this in the inspector.

public Score scoreScript;
public float upgradeValue;
private float moneyPerSecond;
glass hollow
#

Okay, so, post the script as it is just so I can reference it without having to scroll up a bunch

silk rock
#
{
    public Score scoreScript;
    public float upgradeValue;
    private float moneyPerSecond;

    void Update()
    {
     
    }

    public void Addscore()
    {
       if (script.score >= 100)
        {
            script.score -= 100;
            moneypersecond += Moneyperupgrades;
        }
    }

    
}```
glass hollow
#

You also need to change the places you use the variables

#

to the new names

silk rock
#

youre right

runic ore
#

lemon as a side note it would be help to put cs in front of the first `` to make it more readable

#

like so

silk rock
#

cs```public class scorepersec : MonoBehaviour
{
public Score scoreScript;
public float upgradeValue;
private float moneyPerSecond;

void Update()
{
 
}

public void Addscore()
{
   if (script.score >= 100)
    {
        script.score -= 100;
        moneyPerSecond += upgradeValue;
    }
}

}```

#

ah

#

not like this

glass hollow
#

Now, this script wants to add moneyPerSecond to the scoreScript's score variable over time. Right?

Remember several days a go when I said that you can multiply a value in Update by time.deltaTime to make it in units per second instead of per frame?

silk rock
#

yes

glass hollow
#

So, in update, you should add moneyPerSecond to scoreScript's score, making sure to convert it to units per second with deltaTime. You can post just the one line for this one without having to post the whole script again

silk rock
#

is that it cs score += moneyPerSecond * Time.deltaTime;

glass hollow
#

Close

#

Remember that score belongs to scoreScript

#

Not this script

silk rock
#

mb i wrote it in score so i didnt thought i needed script in front

#

but i put in the right one

#

i cant get the cs thing right

#
script.score += moneyPerSecond * Time.deltaTime```
glass hollow
silk rock
#

oh ok

glass hollow
#

I believe all of the compiler errors are sorted, so once you attach this to a button, drag in all the references, it should work

vital fog
#

Script doesn't exist

silk rock
#

yes i fixed that

vital fog
#

Youbnamed it scoreScript

silk rock
#

it works

glass hollow
#

I believe it should be in a functional state but I don't have the compiler in front of me

silk rock
#

yes it works but i cant customize the upgradevalues for each buttons

glass hollow
silk rock
#

yes but it only works when i put the amount in the text that displays the score

glass hollow
silk rock
#

thats the text that displays my score and theres also all the scripts,and this specific upgradevalue is the one that is used and added to the score each second the buttons also have that but if i customize it it doesnt work bc its not the one that is displayed

glass hollow
#

Holy run-on sentences batman.

Okay, so, MoneyCount is the script that tracks and displays your score. Why is scorepersec here? It should be on the buttons

silk rock
#

idk how to phrase it

glass hollow
silk rock
#

so scorepersec doesnt go on moneycount

runic ore
#

i am a 100% beginner in unity and coding in general. but should all those really have their own scripts??? it would probably be way way way easier to just use a GameManager or atleast a MoneyManager and ScoreManager right?

glass hollow
#

It should be on the button that does that

silk rock
#

but now it doesnt work anymore

glass hollow
#

Did you set your button to call the function on its own script now?

silk rock
#

oh i get it

#

yh ima do that

#

one sec

#

i should do a roudn to int bc it displays decimals

glass hollow
silk rock
#

but how do i do that if my score is a float?

glass hollow
#

Like you literally just said

silk rock
#

nvm im just dumb

#

how do i write it to in here cs script.score += moneyPerSecond * Time.deltaTime;

silk rock
#

to roundtoint

glass hollow
silk rock
#

where do i round then?

#

another line?

glass hollow
#

Don't store the rounded value

silk rock
#

oh

glass hollow
#

all you do is change the thing you're displaying

silk rock
#
scoretext.text = score.ToString();```here then
#

thats where im printing it

glass hollow
#

Yes. Set the text to be the rounded value of score

silk rock
#

(RoundToInt(scoretext)).text = score.ToString(); i dont get whats wrong

silk rock
#

me too ngl

glass hollow
#

RoundToInt is a function

#

You pass it a float

#

it gives you an int

silk rock
#

the float i see here is score on the right side

silk rock
#

do i include the ToString

glass hollow
#

If you need to

#

One step at a time

#

Start by rounding your score to an int

#

Then try to set the text to that

silk rock
#

theres an error on an empty line

glass hollow
#

Save the script

#

And if it's still there show the error

silk rock
#

i think i just did it wrong

#

i tried many things

#

am i suposed to state it on another line

glass hollow
#

Show the error

silk rock
#

or in the same line

glass hollow
#

and the line

silk rock
#
      scoretext.text = score.ToString();
      
       score = (Mathf.RoundToInt(score));``` i tried this but doesnt seem to work
glass hollow
silk rock
#

top line displays the score and bottom rounds score to an int?

glass hollow
silk rock
#

makes it equal to score

glass hollow
#

You are setting score to be the rounded value of score. Essentially throwing the fractional part of score into the garbage forever

#

Is that what you want

silk rock
#

i guess not

glass hollow
#

So, what do you want to do. In words

silk rock
#

display the rounded value of score

glass hollow
silk rock
#

scoretext.text = score.ToString();

glass hollow
#

Let's say you want to display the string "foo". What would you write to display "foo"?

silk rock
#

text.text = foo.ToString(): tbh im not sure

glass hollow
#

Not the value of a variable foo, but the literal string "foo"

#

And let's say you're still using the variable scoretext

silk rock
#

honestly idk but ill try scoretext.text = scoretext.ToString(foo)

silk rock
#

no wait

glass hollow
#

Try again

silk rock
#

foo doesnt go there

glass hollow
#

Think about what the line does. What is scoretext.text

silk rock
#

maybe scoretext.text += "foo";

glass hollow
silk rock
#

scorextext is a text

glass hollow
#

Somewhere in the script of that component is a line like public string text;

#

You are setting that variable to a string

#

So step one. How would you set that variable to "foo"

silk rock
#
scoretext.text = "foo";
glass hollow
silk rock
#

(RoundToInt(x))

glass hollow
#

Wrong. Try again.

#

That's the same mistake you made earlier. where did you get this idea so I might find the root cause of this misconception and murder it

silk rock
#

wait

#

i gotta round score

#

int x = (RoundToInt(score)) i feel like i need to put smt before roundtoit but idk what

glass hollow
silk rock
#

int x = (Mathf.RoundToInt(score));

glass hollow
#

Only problem, text is a string. X is an int. Do you know how to get x as a string?

silk rock
#

scoretext.text = (Mathf.RoundToInt(score)).ToString();

glass hollow
#

I was going to have you do it in two lines first then combine them but you put them together ahead of schedule

#

So now, no matter what score is, it will always display it's value rounded to the nearest int, and you can now completely disengage that part of your program from your brain. You will never again have to think about displaying the text, just change score and it's automatic

silk rock
#

great!

#

what would be next step

#

i think it would be to make the main button work

glass hollow
#

Yes, that would be the next step. I'll leave this as homework. Here are some hints:

  1. It should be a separate script, different from either of the two we worked on.
  2. It should be on the button object itself and only reference the score script
silk rock
#

thanks

runic ore
#

holy shit youve been doing these for like 2 hours

#

nice job guys

silk rock
#

youre a great teacher

#

oh shit its 1;15

#

nvm no school tmr iss ok

#

its done

silk rock
#

digiholic

#

@glass hollow i have a question

#

you know yesterday we made a var thats named upgradevalue and it specific to each upgrade,but is it possible to create a place in the score script that tracks the total amount of upgrade value?

#

because upgradevalue is basically my moneypersecond