#Return types and Void

1 messages · Page 1 of 1 (latest)

bronze moat
#

Alright, let's go :)

#
using UnityEngine;

public class VoidVsType : MonoBehaviour
{
    private void Start()
    {
        // Minimum values
        Debug.Log(int.MinValue);
        Debug.Log(float.MinValue);

        // Maximum values
        Debug.Log(int.MaxValue);
        Debug.Log(float.MaxValue);
    }
}
#

This is the first example.
Let's make sure you understand it, and then I will modify it

primal walrus
#

alr

bronze moat
#

Do you know what an int is`?

primal walrus
#

nope

#

should i do the microsoft tutorial

#

real quick?

bronze moat
#

int stands for integer - it is a whole number (no fractions)

primal walrus
#

oh so it is like math

bronze moat
#

You probably should, but I am here now. I'll lead.

primal walrus
#

it is math phrases in english

bronze moat
#

yes, and there are many number types

primal walrus
#

i should understand almost all of these

bronze moat
#

all have their uses

primal walrus
#

just not their english names

#

Float is a number between 0.0000000001-1?

bronze moat
#

Not exactly

primal walrus
#

is that a decimal?

#

oh ok

#

so the int is a whole number

#

The float is any number

bronze moat
#

For beginning game dev, you'll be working mostly with int, float, double
int is a whole number (0,1,2,3,4,5,6,7,8,9,10,11...)
float is a fractional number, appended with an f (1.23f)
double is a fractional number also, appended with a d or nothing at all (the default) (1.23)

#

Do you know how to use Debug.Log() ?

primal walrus
#

so can int both the positiv and negative?

bronze moat
#

That's both a yes and a no.
Some of this matters, but you should not try to comprehend them in that wide a perspective, as a beginner, unless you are also a math genius

#

(Advanced)
There is both int and uint
int is signed, and can thus be positive or negative
uint is unsigned, and uses the extra bit-space to be able to store an even bigger number

#

This is true for most numeric types.
For the record: I have never used uint or any of the unsigned numeric types.

primal walrus
#

Okay, but how would i be able to use this knowledge?

bronze moat
#

I am getting there

Do you know how to use Debug.Log() ?

primal walrus
#

No

bronze moat
#

Alright. Have you written any code so far?

primal walrus
#

in python

#

but it was very basic

#

and i would really only want to learn c#

bronze moat
#

python is super-simplistic, and doesn't deal with types like C#

#

or rather, simplistic might be the wrong word. It's just different.

primal walrus
#

yea that is why i was so confused

bronze moat
#

Have you created a Unity project, or have one open?

primal walrus
#

i have one

#

with a rigged character

#

and movement

bronze moat
#

Any C# scripts in there?

primal walrus
#

yes

bronze moat
#

post one in a code segment or pastebin link

primal walrus
#
using UnityEngine;

public class Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private LayerMask groundLayer;

    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}

#

I wanted movement but no turning

#

and no jumping

bronze moat
#

Alright.
So Update() runs every visual frame
FixedUpdate() runs every fixedTimestep, default 0.2 seconds interval, a value you can specify in the project.

#

0.2 is 50 FPS

primal walrus
#

i understand

#

So if i had everything of my code

#

in update() it would make it very unoptimized

bronze moat
#

inside void update, add Debug.Log(horizontal);
below the line that defines horizontal.

#

actually, no, inside FixedUpdate()

primal walrus
bronze moat
primal walrus
#

oh so

bronze moat
#

polling = checking every frame

primal walrus
#

like

#

lets say i made some movement scripts

#

I would put the keybind in the void update so it would be responsive

#

and the other stuff in the fixed update?

bronze moat
#

Yes, if you're working with Colliders and Rigidbodies

#

Game Logic also goes in Fixed Update

#

not just physics

#

We always check for input in Update

#

However, there is actually an option to let input be registered inside FixedUpdate()
Though, I have never tried it myself.

primal walrus
#

i kinda understand void update is for stuff that can trigger other lines of code?

bronze moat
#

Yes, if you let me lead the conversation I will get you there soon

primal walrus
#

ok

bronze moat
#

Have you added the Debug.Log ?

primal walrus
#
using UnityEngine;

public class Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private LayerMask groundLayer;

    void Update() Debug.Log(horizontal);
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}


bronze moat
#

it needs to be inside the body of the method
and also - FixedUpdate, not Update (I changed my mind earlier)

#

void Update() is a method

#

void FixedUpdate() is also a method

primal walrus
#

private void FixedUpdate(Debug.Log(horizontal))

bronze moat
#

the body is within the curly brackets { }

primal walrus
#

oh

#
    private void FixedUpdate()
    {
        Debug.Log(horizontal)
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
bronze moat
#

Yes

#

Now save the script, press Play in Unity and make sure you see the Console tab in the Editor

#

Debug.Log() is like Console.WriteLine() or print()
it sends strings of text to be displayed in the console

primal walrus
#
using UnityEngine;

public class Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private LayerMask groundLayer;

    void Update();
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal)
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}
bronze moat
#

Yep

primal walrus
bronze moat
#

Ah, forgot one character

#

;

#

after Debug.Log();

primal walrus
#

that is not the problem

#

it is the very last bracket

bronze moat
#

I also see you've added ; after void Update()

#

remove that one

primal walrus
#

see now it works

#

but here is the thing i don't understand

#

why does it say line 21 space 1

#

when it is not the last bracket that is the faulty code

bronze moat
#

Errors are usually very smart, if you've only made one error.
You'll learn to understand them soon enough.
(21,1) means Line 21 Character 1

#

That's where it starts perceiving an error

primal walrus
#

i understand how the problem thing works

#

with 21, 1 thing

#

but that is the last bracket

#
using UnityEngine;

public class Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private LayerMask groundLayer;

    void Update(); <--
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal);
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
} <--
bronze moat
#

what's with the last line? **}

#

it should not be there

#

ah

primal walrus
#

no it was to indicate

bronze moat
#

you wonder what the last bracket is for?

primal walrus
#

to tell the code that that is the end of public class Movement : MonoBehaviour

bronze moat
#

It's best to make actual script comments.

} // <--
primal walrus
#

i used to do it on python

#

so i would understand

#

what i wrote

#

like i would write a bunch of statements and then explain what that part of code was used for

bronze moat
#

that's good practice

#

or you'll have a hard time coming back to said code in 2 weeks

#

Script fixed?

primal walrus
#

yes

bronze moat
#

Alright. Test it, and make sure you see the output in Console

primal walrus
bronze moat
#

Yeah good. And when you move, it shows a number between -1 and 1

#

which is what horizontal returns

primal walrus
#

ok so the horizontal

#

can never be more than -1 to 1

#

but you can increase the force

bronze moat
#

Since input is not smoothed, keyboard input will always be either -1, 0 or 1.

#

The smooth one is GetAxis

primal walrus
#

i understand that means

bronze moat
#

Next up is adding more to the debug log, and doing math inside it

primal walrus
#

0.5f of mouse position x

#

would be half of my monitor

#

right?

#

and that would theoretically scale with any resolution

bronze moat
#

I'm not sure what you're on about now?

primal walrus
#

i am trying to understand

#

the -1 to 1 thing

bronze moat
#

Input.GetAxisRaw("Horizontal") returns -1 when you press to move Left
and 1 if you press to move Right.
If no move is pressed, it returns 0.

It returns an integer, not a float.

primal walrus
#

so i can make the integer more powerful by increase the float value?

bronze moat
#

GetAxis returns a float between -1f and 1f

primal walrus
#

but it will still only be -1 to 1

bronze moat
#

you can't change the input return value, but you'll work around it to get your logic down

#

I'll show you in a bit

#

Now we'll do math in the Debug Log

#
using UnityEngine;

public class Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private LayerMask groundLayer;

    void Update(); <--
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal + " * " + speed + " = " + (horizontal*speed));
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}
primal walrus
#

Replace code?

bronze moat
#

Nah, just replace the Debug.Log line.

primal walrus
#

ok so now instead of showing the int

#

it was showing the float

bronze moat
#

There is also another way to write it, using string interpolation with $

Debug.Log(horizontal + " * " + speed + " = " + (horizontal*speed));
Debug.Log($"{horizontal} * {speed} = {(horizontal*speed)}");
#

It is showing both

#

and the result

primal walrus
#

i see

#

because it is multiplying the current int with the concurrent float

bronze moat
#

Yes. And that is possible, but we should do something to make sure it is precise.

#

If you divide two integers, the result will be rounded down in C#.
If the answer return type is destined to be an integer, you won't get the fractional value

#

so, we cast them

#
Debug.Log($"{horizontal} * {speed} = {((float)horizontal*speed)}");
#

oh actually

#

you're saving horizontal as a float type

primal walrus
#

yea so i can increase

#

or decrease the speed

#

/the power of the int

bronze moat
#

which means inside void Update you are setting an int to a float, which is fine

#

horizontal = Input.GetAxisRaw
which is a value you cannot change

primal walrus
#

because

#

it is an int

bronze moat
#

you can change it after getting it, but every frame it would be reset

bronze moat
primal walrus
#

hm?

#

isn't getaxisraw between -1 to 1

bronze moat
#

Yes, but you store its value as a float, which is fine. There is implicit casting between the basic numeric types.
But if you were to say int myInteger = 1.23f it would round that down to 1

primal walrus
#

I understood it as
GetAxisRaw gives an int value to "Horizontal"

and then it multiplies the "Horizontal" with the float "speed"

bronze moat
#

and then it multiplies the "Horizontal" with the float "speed"
GetAxisRaw doesn't do that

#

or were you referring to the code as it ?

primal walrus
#

not what i meant

#

yes

bronze moat
#

Okay, go on

primal walrus
#

and then the Horizontal multiplied with Speed equals the rb velocity

#

which is the actual movement?

bronze moat
#

as a Vector2 yes

#

Vector2(x,y)

primal walrus
#

and Vector3(x,y,z)?

bronze moat
#

yes

primal walrus
#

Vector 1 is just x?

bronze moat
#

I don't think there is a vector1 😆

primal walrus
#

it would just be x

#

but in theory

bronze moat
#

Vector2 and 3 contain floats

#

but there is also Vector3Int()

primal walrus
#
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
#

What does rb.velocity.y do?

bronze moat
#

on that line you are recreating the velocity vector, and modifying the X axis, but leaving the Y axis as it was previous.

primal walrus
#

oh so

#

if i was to delete that i would go right and up

#

and left and up

bronze moat
#
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
// ...
rb.velocity = new Vector2(horizontal * speed, vertical * speed);
#

That's not the best solution. It would make you go too fast when moving diagonally.
That you can google after this session.

#

if it's even applicable to you right now

primal walrus
#

Is Horizontal seen as code?

#
horizontal = Input.GetAxisRaw("X");
vertical = Input.GetAxisRaw("Y");

bronze moat
#

It is part of the old input system. Typical error is typing "horizontal" because it wouldn't be recognized without the capital H

primal walrus
#

Does it see it like that?

#

i don't mean if i was to replace it

bronze moat
#

it has to be Horizontal or Vertical.

primal walrus
#

But if i write Horizontal does it just see it as X or is it a name im giving it

bronze moat
#

Input.GetAxisRaw("Horizontal") returns a very specific variable from the Unity input system

When you do horizontal = that
You're just copying the value (at a point in time) and storing it in a variable.

That is because float is a value type
as opposed to reference type.

Classes are reference types.
When you write: MyClass instance_1 = new MyClass();
And then write: MyClass secondVariable = instance_1;
It doesn't make a copy, but copies the reference (path) to that class instance.

#

But when you work with value types, it makes copies, at that specific point in time.

#

string, int, float, double, Vector3 and all that are value types

primal walrus
#

oh so

#
//horizontal is now if referenced = Input.GetAxisRaw("Horizontal");
#
horizontal = Input.GetAxisRaw("Horizontal");
#

tell me if this is correct, but it sees that line of code and then everytime "horizontal" is used it will actualy use this string of code?

bronze moat
#
private void Start()
{
    horizontal = Input.GetAxisRaw("Horizontal");
    // This would give you the value of GetAxisRaw only at the first frame. They would not otherwise be "connected" to the same reference. 
}
bronze moat
primal walrus
#
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal + " * " + speed + " = " + (horizontal*speed));
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
#

horizontal does not mean anything

#

but i gave it a value

bronze moat
#

it is whatever value it was last given

primal walrus
#

that it is now using in FixedUpdate

bronze moat
#

void Update() can run like 300 FPS
FixedUpdate() only runs 50 FPS
So you get whatever value was last inputted.

primal walrus
#

I see so i can change the value of horizontal

#

many times in the script

bronze moat
#

yes, but it is best to add more variables, so the original value remains unchanged, in case you need it later

primal walrus
#
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal + " * " + speed + " = " + (horizontal*speed));
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }

         void Update()
    {
        horizontal = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        Debug.Log(horizontal + " * " + speed + " = " + (horizontal*speed));
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
#

so in this the first FixedUpdate horizontal is equal to the equation in the first void update

bronze moat
#

the last void update that ran before the current FixedUpdate

primal walrus
#

yea so the first

#

but the second fixed update would have the value of the second void update

#

since it changes it's value?

bronze moat
#

They don't run in tandem, though there is some internal execution order logic

#

If void Update runs 300 times per second
and Fixed runs 50 times per second
That means Update will run 6 times before the first Fixed is called.

#

So whatever Update gave horizontal last, is what Fixed will use.

primal walrus
#

oh

#

so if i wanted the horizontal to have different values

#

i would have to write it in the fixed update

bronze moat
#

or later in void update

#

Fixed is better for performance, if the logic permits it

#
using UnityEngine;

public class VoidVsType : MonoBehaviour
{
    private void Start()
    {
        // Minimum values
        Debug.Log(int.MinValue);
        Debug.Log(float.MinValue);

        // Maximum values
        Debug.Log(int.MaxValue);
        Debug.Log(float.MaxValue);
    }
}
#

This is the first script example I made

#

Now I'll show you where it leads

#

related to void

#
using UnityEngine;

public class VoidVsType : MonoBehaviour
{
    private void Start()
    {
        MinValues();
        MaxValues();
    }

    private void MinValues()
    {
        // Minimum values
        Debug.Log(int.MinValue);
        Debug.Log(float.MinValue);
    }

    private void MaxValues()
    {
        // Maximum values
        Debug.Log(int.MaxValue);
        Debug.Log(float.MaxValue);
    }
}
primal walrus
#

hmm

bronze moat
#

voids are just pockets of logic

#

one of the primary principles in electronic data management

primal walrus
#

when you write

bronze moat
#

is to never repeat yourself, by storage or by code

primal walrus
#

private void MaxValues()

#

is private void the logic of the execution

#

and MaxValues is just the name of this execution?

bronze moat
#

So if you need to repeat some line of code, you can call it

    private void SomePocketOfCode()
    {
        // Logic here
    }

and repeat that anywhere.

#

Yeah, you can give it any name you want

#

there are preferred conventions

#

It's best to use language that helps the code make sense

#

call things exactlyWhatTheyAre

primal walrus
#
    Don't repeat unless called()
    {
        // Logic here
    }
#

is there anyway to do that?

bronze moat
#

Class, Property and Method names usually go with PascalCase
While internal variables, and some public, use camelCasing.

bronze moat
#

underscore

primal walrus
#

but will it work?

bronze moat
#

also, no apostrophes

#

Will what work how?

primal walrus
#

if i wrote code into that it wouldn't run unless called?

bronze moat
#

That means nothing to the compiler, only to you.

#

But you decide when to run it

primal walrus
#

like Void update will update every frame

bronze moat
#

making a void AnyNamePossible() doesn't make it run unless you call it somewhere

primal walrus
#

and private void will update every set amount

#

so a void doesn't run unless it is needed

bronze moat
#

only void Update is automatically called every frame

#

Unless you specify it to run.

primal walrus
#

omg

#

yes

bronze moat
#

These are some of your primary options for executing code in MonoBehaviour

primal walrus
#

thank you

bronze moat
#

Very good read. Absolute bookmark.

primal walrus
#

this is the stuff that makes no sense

#

because it has no relation to python

bronze moat
#

MonoBehaviour is a Unity class, designed to work with the engine.
MonoBehaviour scripts need to be attached to an instance of a GameObject
For background systems, there are other options, like MonoBehaviour singletons (static instance) or regular C# classes and statics.

primal walrus
#

ah

bronze moat
#

statics can be accessed from anywhere, without needing a reference

primal walrus
#

monobehaviour is kinda like unity code

#

in some way

bronze moat
#

Yes, exactly

primal walrus
#

that is why i was so confused when i opened visual studios

#

open new c# files

#

the stuff i need for unity is not there

bronze moat
#

indeed

#

on that note, is your IDE configured?

#

does MonoBehaviour highlight green?

primal walrus
#

yes

bronze moat
#

ok good

primal walrus
#

but

#

the colour is kinda annoying

#

because the values i preset are not white all the time

bronze moat
#

Never heard that one before.
If it really is a problem, perhaps try lowering the brightness of your monitor.

bronze moat
primal walrus
#

for me to understand the code i have to in my head try to interpret it as a number

#

but it is difficult when it doesn't stay consistent

bronze moat
#

hmm

#

sec

primal walrus
#

What i assign i want in white text

bronze moat
#

Add this anywhere inside Update()

float test = 1f;
Debug.Log(test);

No need to run it.

#

screenshot how it looks

primal walrus
bronze moat
#

is that Visual Studio Code?

primal walrus
#

Yes

bronze moat
#

This is how it looks in Visual Studio Community

#

2022

primal walrus
#

see

#

all the code you assign is white

bronze moat
#

Community is objectively better from a beginner perspective, as far as I have understood.

#

not about coloring

#

The code I assign which is of the class, is white.
But local variables, that only exist inside their respective method, are tinted blue.

primal walrus
#

i have a bigger issue

#

than the color thing

bronze moat
#

Being?

primal walrus
#

let's say

#

private. something

#

when i type the .

#

it automatically picks something else

bronze moat
#

I am not a user of Visual Studio Code, and cannot relate.
Also, it is beside the point of this thread*, and beyond the scope of the channel code-beginner.
General questions go in #💻┃unity-talk or any other specific channel that covers them.

#

Let me be frank: I am trying to help* you understand code before I have you run some very specific lines of code, which I believe will help you understand some basics of Unity, while it covers the significance of voids and data types.

#

This derailing is costing a lot of time.

#

I can help you make an FPS counter that displays with text you can stylize

#

in the game

#

Which version of Unity is your project running?

#

2019, 2020, 2021 or beyond?

#

@primal walrus FYI, I am not frustrated, just getting hungry.
This will take just 5-10 minutes, and you'll have a working example with void, int, float and string.

primal walrus
#

2021

bronze moat
#

Alright. If I recall correctly, that means it ships default with TextMeshPro.
We're going to create a UI element, that is to be rendered by the camera.

#

Can you see the Text - TextMeshPro option?

#

when right-clicking in the Hierarchy

primal walrus
#

yes

bronze moat
#

Create one

#

It will automatically also make a Canvas and an EventSystem

#

There needs to be at least one EventSystem for buttons etc to work.
Every UI element needs to be inside a Canvas.

#

You can't see the text easily in Scene View. Go to Game* view, and edit the text inside the Text component of the object.

primal walrus
bronze moat
#

Great. Now, we're going to access that text by code.

#

in your script, on the top, add using TMPro;

primal walrus
#

🤨

bronze moat
#

?

primal walrus
#

just create a new script?

bronze moat
#

No, in the same script

#

or, you could create another

primal walrus
bronze moat
#

Great

#

Add using TMPro;

#

and make a private TMP_Text uiText;

#

~~rename Start() to Awake() ~~

#

actually

#

no

primal walrus
#

Add using TMPro; where i put this

bronze moat
#

Awake vs Start
When accessing a different object, it has to be done in Start, because Start runs after all other scripts have run Awake (or they won't be found anyway)

#

top of the script, next to the other using statements

primal walrus
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Add using TMPro;

public class FPSCounter : MonoBehaviour
{
    void Start()
    {
    
    }

    void Update()
    {
        
    }

    private TMP_Text uiText;
}
bronze moat
#

I think that 4th line is causing an error

primal walrus
#

it is

bronze moat
#

The reason should be obvious 🤔
But I could point it out

primal walrus
#

using TMPro;

bronze moat
#

Alright.
Btw, it is usually preferred to have the variables, like uiText, above the methods.

primal walrus
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class FPSCounter : MonoBehaviour
{
    private TMP_Text uiText;
    
    void Start()
    {
    
    }

    void Update()
    {
        
    }
}
bronze moat
#

Next up - in the editor, select the text object in the hierarchy, scroll down in inspector, and choose Add Component - type 'FPSCounter'

Then add this line in Start()

uiText = GetComponent<TMP_Text>();
#

Now you can change its text by uiText.text = "my own string of characters"

primal walrus
#

i don't see Start()

bronze moat
#

that's in the script

primal walrus
#

oh

bronze moat
#

you have attached the MonoBehaviour to a GameObject, which means that game object will have its own instance of the script

#

If this weren't for UI, but enemies, each enemy could have its own instance of that same script

#

btw it's taking a few more min here because I have to write a new FPSCounter from scratch

#

add these variables

private float timer;
private int frames;
private float framesPerSecond;
primal walrus
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class FPSCounter : MonoBehaviour
{
    private float timer;
    private int frames;
    private float framesPerSecond;
    private TMP_Text uiText;
    
    void Start()
    {
        uiText = GetComponent<TMP_Text>();
    }

    void Update()
    {
        
    }
}
bronze moat
#

in the simplest form, an FPS counter is

void Update()
{
    uiText.text = 1f/Time.deltaTime;
}

But it would change every single frame, which isn't very nice.
You can try it out. I'm typing out how to only update it every 1 second

#

Time.deltaTime is the time in seconds since the last frame was called.

#

it is a float

#

I realize now, this is missing from your movement script.
When you set speed to be 8f, you mean to say 8f per second.
In order to make that distribute across the frames during that second, we multiply 8f by Time.deltaTime

primal walrus
#

Cannot implicitly convert "float" to "string."

bronze moat
#

Ah, that is true.
Two ways to fix this:

#
void Update()
{
    uiText.text = (string)(1f/Time.deltaTime);
    // or
    uiText.text = (1f/Time.deltaTime).ToString();
}
#

I suggest ToString() because you can do ToString("F2") and get only 2 decimals

primal walrus
#

well i have 80 fps

#

capped

#

for some reason

#

is this just unity settings?

bronze moat
#

That is a project setting.

primal walrus
#

okay no problem

bronze moat
#

From my personal FPS Counter

if (setCustomFrameRate)
{
    customFrameRateWasTrue = true;

    // Save original settings
    originalTargetFrameRate = Application.targetFrameRate;
    originalVSyncCount = QualitySettings.vSyncCount;

    // 0 for no sync, 1 for panel refresh rate, 2 for 1/2 panel rate
    QualitySettings.vSyncCount = 0;

    // vSyncCount must be zero
    Application.targetFrameRate = targetFrameRate;
}
#

Application.targetFrameRate and QualitySettings.vSyncCount

#

I actually don't know where else in the editor it is changed.

#

Anyway

primal walrus
#

how was it i made the void update slower

#

for the fps counter

bronze moat
#

This code I just posted was not inside Update, but OnEnable()

#

only runs every time the object is Enabled

#
using UnityEngine;
using TMPro;

public class FPSCounter : MonoBehaviour
{
    private TMP_Text uiText;
    private float timeCount;
    private int frameCount;
    private float framesPerSecond;
    private float pollingRate = 1f; // Update interval in seconds

    void Start()
    {
        uiText = GetComponent<TMP_Text>();
    }

    void Update()
    {
        timeCount += Time.deltaTime;
        frameCount++;

        if (timeCount >= pollingRate)
        {
            framesPerSecond = frameCount / timeCount;
            uiText.text = framesPerSecond.ToString("F2");

            timeCount = 0;
            frameCount = 0;
        }
    }
}

#

There you go.

#

If pollingRate is 10f, the result will be the average FPS over 10 seconds

#

Any questions?

#

Forgot to explain earlier: TMP - TextMeshPro is the fully adopted successor to Unity's default UI (which looks horrible)

#

In older versions it has to be imported as a Package

#

default UI text is pixelated etc

#

@primal walrus

#

To explain the void - you could split up the code inside update, into several individual methods,
and then simply call those methods in the correct order, inside Update, and the functionality would be identical.

#

To explain return types - void is just a set of code to run
Methods can also return types that have been computed inside said methods.

private void MyMethod()
{
    Debug.Log(MyText());
    Debug.Log(MyNumber())
    Debug.Log(MyNumber() / 2)
}

private string MyText()
{
    return "Some random text";
}

private int MyNumber()
{
    return 4;
}
#

There are multiple ways to reach the same results in C#.

private int myNumber = 4;
private int MyNumber()
{
    return 4;
}

These are identical, and there is also Properties (with custom get and set), but those aren't really important right now.

#

Methods can also take arguments, so you can pass along external values.

private void MyMethod()
{
    Debug.Log(MyNumberMultiplied(6))
}
private int MyNumberMultiplied(int multiplier)
{
    return 4 * multiplier;
}
#

Well, it's been 3 hours. Good luck 👋

bronze moat
#

@primal walrus Did you read the last part of the thread?

#

I will be closing the thread by the end of the day.