#Null error

1 messages Β· Page 1 of 1 (latest)

loud salmon
#

This is very confusing

#

Also ima start a thread cuz this is taking longer then anticipated, and it's useful to keep everything together

#

If you want you can take a break now and we can look at it later

boreal sand
#

yes i understand

#

yes can we i been sitting on like 3 hours with no solution

loud salmon
#

Yeah game development be like that sometimes hah

boreal sand
#

i am just so frustrated :/

loud salmon
loud salmon
boreal sand
#

yup thanks for trying to help me

#

hahah

loud salmon
#

Have a nice break :p

boreal sand
#

im ready if u r

#

@loud salmon

loud salmon
#

Sure

boreal sand
#

maybe is smart to tell u What i am doing

loud salmon
#

Okay so, on what object is the MoreHP script?

loud salmon
boreal sand
#

the blue is my player bullet

#

the red aliens are shooting from my green box

#

the purple on top is shooting yellow bullets

#

they can tak dmg of my earth and my player my earth has 10 hp and i have 3 hp

#

i want to make it so when i shot the red bullet i gain hp

loud salmon
boreal sand
#

and if the enemy hits the red it only gets destroyer

loud salmon
#

I don't get the last one

boreal sand
#

the enemey is on top of the screen shot they shot "yellow bullets"

loud salmon
#

Yes I got that

loud salmon
boreal sand
#

the green box to the side

loud salmon
#

Right

#

So, your blue bullet

#

Has to exactly collide

#

With the red bullet

boreal sand
loud salmon
#

And the red bullet, has the MoreHP script

#

Correct?

boreal sand
#

yes

loud salmon
#

There's your problem

boreal sand
#

the yellow bullets can destroy the red one to but then the player DONT get healing

#

if player can shot the red bullet then u GET the healing

loud salmon
#

Are the purple ones allies then?

boreal sand
#

thats why in the red bullet scrip (MoreHP) i chek if the BULLET hits the red bullet

loud salmon
#

Yes, that makes sense

boreal sand
#

yes

loud salmon
#

Only your problem is that in that case

boreal sand
#

i dont want it to be easy to get the healing thats why they aliens can aslo hit it

loud salmon
#

The OTHER is the RED BULLET and the RED BULLET does not have the PlayerMovment Component, therefore, Player == null

#

Also, please rename PlayerMovment to PlayerMovement

#

Do you understand what I'm saying, or do I've to elaborate?

boreal sand
loud salmon
#

Right

#

Okay

#

Let me just make a drawing in MS Paint to illustrate

boreal sand
#

while u draw lett me just fixe the naming didnt see it before now

loud salmon
#

Cool

#

Okay just to recall, which bullet carried the MoreHP script?

#

Just so I'm sure of it

boreal sand
#

red

#

blue has tag

#

yellow u dont need to worry about

loud salmon
#

Alright

boreal sand
#

here the 1 log u see is the log i got when my bullet hit the red the other 2 is on this picture

loud salmon
#

Does this help?

#

Green Circle is the player, firing the blue bullet, Dark green square, is the enemy, firing the red bullet

boreal sand
#

oh i see

loud salmon
loud salmon
#

It's not 2 players and 2 enemies

loud salmon
boreal sand
#

so in sted of playermovment i redirect to the blue bullet?

#

script

loud salmon
#

I mean it's already borked that your health is part of your playermovement

#

But the problem is that you want the playermovement, to adjust the health

#

But you can't because the blue bullet doesn't carry that script

#

So if you try to get the component, it fails, because it simply doesn't have the component

boreal sand
#

yea i understand kinda but somthing just dont make sense" tries to get the playermovment component from the blue bullet, witch it dosnt have" it goes from the red bullet script directly to player dont have any script in the blue bullet other then movment of it

#

OOOOH NVM NOW I UNDERSTAND

loud salmon
#

:)

#

If the bullet collides with the player

#

It can get the script

#

I can draw that out too if you want :D

boreal sand
#

let me test cheking if i move the firepoint of my gree squere what happends if it hits the player

#

then i need to give player the same tag

loud salmon
#

I do not understand what you mean.

boreal sand
#

u see what hight the red bullet is shoting at

#

i just moved it TO TEST what happends if the red bullets hits the player if it THEN give health or not

#

u are corect

#

it works if the player hit the red bullet

loud salmon
#

This btw to illustrate why the other does work

boreal sand
#

yea i understood if u look at my picture u see i gain hp

#

i start at 30 on my player

#

PAIN

loud salmon
#

:)

boreal sand
#

is ther any fix to this?

loud salmon
#

Well, obviously, you won't do a GetComponent hah

#

I mean, I'd first just seperate your health, from the PlayerMovement

#

That'd be a good start

boreal sand
#

but how do i gain health then?

#

if i sepreated them

loud salmon
#

Through a seperate script

#

That's not attached to your movement

#

It's attached to your player

boreal sand
#

again i am sry i am feeling so dumb

loud salmon
#

You've two scripts on your player then

#

One for health, one for movement

boreal sand
#

the player movment is attached to my play

#

unless u mean 2 script on player

#

files*

#

one health one acually movment

loud salmon
boreal sand
#

ah i see

#

but wont it mess with my takedmg script

loud salmon
#

If my unity would actually boot up I'd give you an example lol

boreal sand
#

NO NVM NVM

loud salmon
#

kek

boreal sand
#

this will be pain full 😦 alright i msg u when i am done lmao

loud salmon
#

Here, my player has 4 scripts

loud salmon
#

Like if you think of it logically as well, it doesn't make sense that the enemy needs a reference to your player's movement, whilst it needs to only deal damage to you

#

It's not adjusting your movement whatsoever

loud salmon
#

Here, in this case, my player loses a life, if it collides with anything that is tagged as Obstacle

boreal sand
#

i just liuke having 1 folder for each object but your way is more "clean" if that makes sens

boreal sand
#

why static public and public static

#

mentscript file

loud salmon
#

oh the ints are static due to convenience because unit tests are a pain, it was for an assignment

#

And you can ignore the Actions

boreal sand
#

aah ok

loud salmon
#

The health should be a private int really

boreal sand
#

mine?

loud salmon
#

maybe public, depends on how you use it

#

No my case

boreal sand
#

aaah ok

loud salmon
#

It depends if you've reference it or not

boreal sand
#

i see

loud salmon
#

Like, you could technically also write a function to get your health, whilst it still being private:

#

There are many ways to code things :)

boreal sand
#

ooooh

loud salmon
#

You could also choose to create a Singleton out of your upcoming Health class

#

It'd make it so that you'd not have to use GetComponent

#

Also steam is eating my pc lol

boreal sand
#

yikes

#

i always force close steam and other game engines they so good at that

#

are u german?

loud salmon
#

No, but close, Dutch

boreal sand
#

ah

loud salmon
loud salmon
#

If you're curious, this is what I did for my health class:

using System;
using Extensions;
using UnityEngine;
using UnityEngine.UI;
using static Constants.Const;
using System.Diagnostics.CodeAnalysis;

namespace PlayerHealthSpace
{
    public class PlayerHealth : MonoBehaviour
    {
        #region VARIABLES
        private float previousHealth;
        [SerializeField] private int maxHealth;

        [SuppressMessage(Style, NamingStyle, Justification = Pending)]
        public static event Action<float> onValueChanged;

        private Slider Slider { get => Instance.Slider; set => Instance.Slider = value; }
        private float Health { get => Slider.value; set => Slider.value = value; }
        #endregion

        private static PlayerHealth Instance;
        private void Awake() => Instance.Singleton(() => Instance = this);

        private void Start()
        {
            Slider = GetComponent<Slider>();
            Slider.SetToMax(maxHealth);
        }

        private void ModifyHealth(float amount)
        {
            Health += amount;
            onValueChanged(Health);
            GameEvents.PlayerHealthEvents.OnPlayerHealthChanged(Health, previousHealth < Health);
            Slider.OnZero(GameEvents.PlayerHealthEvents.OnPlayerDeath);
            previousHealth = Health;
        }

        public static float GetHealth() => Instance.Health;
        public static void IncreaseHealth(float increaseAmount) => Instance.ModifyHealth(increaseAmount);
        public static void DecreaseHealth(float decreaseAmount) => Instance.ModifyHealth(-decreaseAmount);
    }
}
#

The UI of it also a seperate class

boreal sand
#

to advanced yet for me

loud salmon
#

I know, but it's fun to show

boreal sand
#

i see som stuf i have like slider

#

i have that from my earth

loud salmon
#

Ah nice

#

But if you want you can use a singleton, that's not too advanced

#

It'd make a great deal of stuff easier probably

boreal sand
#

if u think i should use it i can try

loud salmon
#

Alright, first though, how is your health script coming along?

boreal sand
#

just finish tested and working

loud salmon
#

Mind showing?

boreal sand
#

WELL not the get hp

#

but the losing does

loud salmon
#

Fair yeah

boreal sand
#

script or the inspector

loud salmon
#

Script please

boreal sand
#
public class PlayerHP : MonoBehaviour
{
    public int HP = 30;
    public SpriteRenderer m_SpriteRenderer;
    public Color m_NewColor;


    public void TakeDamage (int damage)
    {
        HP -= damage;
        if (HP <= 0)
        {
            Die();
        } else {
            UpdateColor();
        }
    }

    public void GetHP(int GiveHPToPlayer)
    {
        HP += GiveHPToPlayer;
        UpdateColor();
    }

    public void UpdateColor ()
    {
        if (HP == 30)
        {
        m_NewColor = new Color(0,255,27);
        m_SpriteRenderer.color = m_NewColor;
        }
        else if (HP == 20)
        {
        m_NewColor = new Color(249,255,0);
        m_SpriteRenderer.color = m_NewColor;
        }
        else if (HP == 10)
        {
        m_NewColor = new Color(255,0,0);
        m_SpriteRenderer.color = m_NewColor;
        }
    }

    void Die ()
    {
        Destroy(gameObject);
    }
}

#

figured better to do it in script than send u a new link

#

unless u want a link

loud salmon
#

Nah it's fine in here because you don't completely blast the global chat lol

loud salmon
#

So good job

boreal sand
#

need to start writing the coments on the scripts later πŸ˜…

loud salmon
#

Oh yeah, do that, I've definitely stared at my code for longer thant 15 minutes sometimes to figure out what the actual fuck my code did lol]

boreal sand
#

haha

#

ok now what did u want me to do?

#

singelton

loud salmon
#

Yeah

#

Lemme first help out your updatecolor though

#

Like, you use methods, you can also use methods within methods

#

And, ya see, this is a lot of repeating code:

    public void UpdateColor ()
    {
        if (HP == 30)
        {
        m_NewColor = new Color(0,255,27);
        m_SpriteRenderer.color = m_NewColor;
        }
        else if (HP == 20)
        {
        m_NewColor = new Color(249,255,0);
        m_SpriteRenderer.color = m_NewColor;
        }
        else if (HP == 10)
        {
        m_NewColor = new Color(255,0,0);
        m_SpriteRenderer.color = m_NewColor;
        }
    }
#

Whenever you see repeating code, you've to look for patterns, and if there are patterns, you can use methods to reduce said repeating code

#

So say like, a method of HealthStage

#

You could do:

boreal sand
#

u mean m_SpriteRenderer.color = m_NewColor; make it to a word then use that insted of same line?

loud salmon
#

nop

#
private void HealthStage(int StageHP, Color stageColor)
{
    if (HP == StageHP)
        {
        m_NewColor = stageColor;
        m_SpriteRenderer.color = m_NewColor;
        }
}

 public void UpdateColor ()
    {
        HealthStage(30, new Color(0,255,27));
        HealthStage(20, new Color(249,255,0));
        HealthStage(10, new Color(255,0,0));
    }
#

:)

#

And yes, this could probably be even better, but this is fine for now

boreal sand
#

wont i have to make stagehp a variable

loud salmon
#

It is already

#

It's a parameter

boreal sand
#

ooh i i am blind

#

...

loud salmon
#

:p

#

You could also save those new colors as variables and put them in as variables

boreal sand
#

sry but how does healthstage talk to updatecoloor after the if statment

loud salmon
#

What do you mean exactly?

boreal sand
#

nvm

#

private only a thing for diffrent files

#

thought it was for same files to

#

that what every u write in private stays there

#

if that make sens

loud salmon
#

private makes it so that it's only accessible to the class that it is in

#

public makes it accessible to everything that has a reference to the class

boreal sand
#

yea ok gotchu

#

again i am sry

#

u have to putt up with this

loud salmon
#

Don't be sorry lol

#

You're here to learn

loud salmon
#

I enjoy teaching people stuff

boreal sand
#

πŸ˜…

loud salmon
boreal sand
#

yea

loud salmon
#

And do you understand why it works?

boreal sand
#

i do u explained it well on the private only thing i was "scared of"

loud salmon
#

Hah

#

HealthStage is private since you'll never use it outside of the class

#

If you do want to, well you're problem doing some weird stuff that you shouldn't be doing

loud salmon
boreal sand
#

NO nvm another thing i dont under stand how does stageHP actually stay the same value as my hp

#

u said only its a int

loud salmon
#

What do you mean?

boreal sand
#

but not that its == 30 or ayntihg

loud salmon
#
HealthStage(30, new Color(0,255,27));
boreal sand
#

oh no nvm

#

read wrong

loud salmon
#

it still does HP == 30

#

But 30 is as the StageHP parameter

boreal sand
#

yea

#

understood

#

that acually makes very much sens easy to read

loud salmon
#

:)

boreal sand
#

guessing this is the next step?

loud salmon
#

I mean it's fine actually, just the naming is a bit weird

#

It could AddHP

boreal sand
#

oh u mean gethp? or the givehptoplayer

loud salmon
#

Or IncreaseHP

#

Or HealPlayer

#

Get sounds like you specifically want to receive the variable

boreal sand
#

i need to add max hp to πŸ˜… but thats easy i just do it later

loud salmon
#

Lets do the Singleton first, that'll make life easier

boreal sand
#

yes

#

read a litle bit about it now

loud salmon
#
    public static PlayerHP Instance;
    private void OnEnable()
    {
        if (Instance == null)
            Instance = this;
        else Destroy(gameObject);
    }
#

This'd be a Singleton

#

Now, if you need to say like do TakeDamage() in another script because e.g. Collision

#

You can refer to it as PlayerHP.Instance.TakeDamage()

#

Without needing to do a GetComponent

boreal sand
#

oh i read in articale i read said

public class Singleton : MonoBehaviour 
{
    public static Singleton instance;
} ``` is the singleton play hp in this case
loud salmon
boreal sand
#

this should be in the red bullet script no?

loud salmon
#

And there's an else thhat will destroy the gameobject if there are duplicates

loud salmon
#

Singletons are used for things that only have ONE instance, not more not less.

boreal sand
#

ahh

loud salmon
#

else Destroy(gameObject);

#

That's why this else is here

#

It's a failsafe

#

If there are more then one

#

Destroy yourself.

#

But given that you'll always only have ONE PlayerHP, you can use a Singleton for it

#

I'd also recommend having a namespace, since you use a Singleton

#

You probably have to format it differently

#

The bot doesn't like it

boreal sand
#
if (Instance == null)
            Instance = this;
``` what does "this"
boreal sand
#

oh

loud salmon
#

this refers to itself

#

You can use it for these things as well, because this wouldn't work obviously

#

But this does

boreal sand
#

ooo

loud salmon
#

Also, this is a namespace:

#

So, if I want to refer to my health in say like my score class

boreal sand
#

but cant u do that if u write the file name?

loud salmon
#

You'd need a using PlayerHealth

boreal sand
#

isnt it the same

boreal sand
#

namespace isnt it the same as what i do here

loud salmon
#

Here, this works because I've this

boreal sand
#

calling the file

loud salmon
#

If I remove it, it won't work

loud salmon
boreal sand
#

oooh

#

no get u

loud salmon
#

We are using a Singleton to prevent having to use GetComponent

boreal sand
#

namespace does the same as that just for singleton

loud salmon
#

No, namespace is just to limit where you use it

#

Only classes with using <namespace> can use the class

boreal sand
#

aaah

loud salmon
#

It's just cleaner coding, and it's definetly good for if you're using Singletons, since with a Singleton you can acces the stuff always

#

Without needing a reference

#

also brb

boreal sand
#

wait ok u want me to make a namespace what should i call it

boreal sand
loud salmon
#

You usually don't want it exactly as the classname

boreal sand
#

done and cleaned code up a litle

#

just spacing and that not all names i do it later so i wont waist your time

loud salmon
#

lol

boreal sand
#

if u saw the other files it would be 100 of names calleds testing 1 2 3

#

to lazy thinking of names

loud salmon
#

Lol, naming is important though

boreal sand
#

when i am in the coding zone

#

yea i know

loud salmon
#

Hah

#

Bad habit ;)

boreal sand
#

thats what happend when there is not teacher to teach u how to write a "clean code FROM THE START" first to years

loud salmon
#

Kek

#

Anyways, you can rework your collision code now

#

It no longer needs to do a GetComponent

#

So this,

PlayerMovment Player = hitInfo.GetComponent<PlayerMovment>();
        Debug.Log(Player);
        if (Player != null)
        {
            Debug.Log(Player);
            Score.myScore += -10;
            Player.TakeDamage(damage);
        }

Can be this:

Debug.Log(Player);
Score.myScore += -10;
Player.Instance.TakeDamage(damage);
        
boreal sand
#

in the moreHP script right?

loud salmon
loud salmon
boreal sand
#

cool stuf makes more and more sense

#

😁

loud salmon
#

:D

boreal sand
#

wait...

loud salmon
#

?

boreal sand
#

Debug.Log(Player);
Score.myScore += -10;
Player.Instance.TakeDamage(damage); this is takemdg not give

#

i cant take dmg when i destroy red bullet

loud salmon
#

Well, it's for your TakeDamage as well

boreal sand
#

in red bullet??

loud salmon
#

Noooo

#

I meant just the normal bullet

#

If it hits you

#

It deals damage

boreal sand
#

ooh

loud salmon
#

That can also now be reworked

boreal sand
#

so i change that also

#

AAAH

loud salmon
#

It no longer needs to GetComponent<>

#

Yes

boreal sand
#

slow brain to day

loud salmon
#

Nah, I feel like I was also being unclear there hah

#

I mean, I've already given you like a lot of infromation

#

So you're pretty well with keeping up

boreal sand
#

problem

loud salmon
#

And that is

boreal sand
#

the middle wouldnt that mess with the other score i have?

#

this looks wrong

loud salmon
#

You probably want a tag check for the player

#

I assume you only want the player to take damage if the bullet hits the player

boreal sand
#

cuss u suposed to not gain points when the objects hits u or the earth

#

yes and if it hits the earth it would lose hp also

loud salmon
#

Yes but they are two seperate healtbars right?

boreal sand
#

yes

loud salmon
#

Oh

#

Your way of filtering it in the old way worked

#

But it's just really fkin weird lmao

boreal sand
#

wait what do u mean

loud salmon
#

Your old system would filter by if it failed the GetComponent

#

Which is interesting

#

Does your Player have a tag?

boreal sand
#

nope

#

just have if any object trigger

loud salmon
#

Give your Player a tag called "Player"

boreal sand
#

same with earth? or should i lett eath be

loud salmon
#

No, if you'd do that it'd trigger earth as well

#

You want

if(hitInfo.CompareTag("Player")
{

}

Around this

loud salmon
boreal sand
#

u mean if(randomname.CompareTag("Player")

#

just need a good name for what it does?

loud salmon
#

Well, yeah, it's your local variable of the OnTriggerEnter2D

boreal sand
#

ok

loud salmon
#

You check to other's tag

#

The object it collides with

boreal sand
#

brb 1 min

#

(creative name ik ) i named my tag player with smal p that wouldnt mess anyting up right?

loud salmon
#

But I'd make tags with a capital

boreal sand
#

no no the tag is player and whats in the compare tag is also player neither is with Capital p

#

= they are the same

loud salmon
#

Yeah I know

#

But I mean, you should name your tags with a capital

boreal sand
#

oh ok

loud salmon
#

All default tags are PascalCase as well

#

Code conventions

boreal sand
#

uhm shouldnt i change on trigger hit info and the one on earth

loud salmon
#

hitCheck doesn't exist?

#

Is your IDE configured?

boreal sand
#

nope dosnt exist then i change all of them

loud salmon
#

You know you can rename things right?

loud salmon
boreal sand
loud salmon
#

ah

boreal sand
#

having problem making tag i am writing it but its not getting made jsut give me a sec

loud salmon
#

Oof

boreal sand
#

BROOOO it worked but i mis typed

#

now i cant make it same isue

loud salmon
#

What

boreal sand
#

not allowed to name the tagg with big p

#

because i have somting already called Player

#

this

#

fuck the time its 15 min to 3 am and i have school

#

πŸ˜…

loud salmon
#

F

#

Go to sleep lol

loud salmon
#

You can use that

boreal sand
#

ok i do that

loud salmon
boreal sand
#

not this

#

wait i just do the same on the more hp script

loud salmon
#

This no longer exists

loud salmon
loud salmon
#

If your IDE would be configured

boreal sand
#

the debug is gone but i still have the rror

#

its because of the others

loud salmon
#

Oh wait

#

It's PlayerHP.Instance

boreal sand
#

what about the damge also error there

#

i lied

#

no dmg

#

ohmy i gain hp now

loud salmon
#

:D

boreal sand
#

thank you so much

loud salmon
#

Yw :)

boreal sand
#

i just have 1-2 more question (very small)

loud salmon
#

Sure

#

Ask away

boreal sand
#

when i gain back to 30 hp it dosnt turn the green coller i said u should

#

it give this collor

loud salmon
#

Well, then it was always broken to begin with because it is the same code kek

#

Only in a method

#

What was the exact code again?

boreal sand
#

like a blue coller

#

it gives me cyan

#

not green

loud salmon
#

I need to see the code lel

boreal sand
#
private void HealthStage(int StageHP, Color stageColor)
{
    if (HP == StageHP)
        {
        m_NewColor = stageColor;
        m_SpriteRenderer.color = m_NewColor;
        }
}

 public void UpdateColor ()
    {
        HealthStage(30, new Color(0,255,27));
        HealthStage(20, new Color(249,255,0));
        HealthStage(10, new Color(255,0,0));
    }
 
#

insted of 0,255,27 the code in unity turns to 0,191,21

loud salmon
#

What is in HealthStage

boreal sand
#

that?

#

update above

#

it not coding mistake i had that problem before its omthing on unity

#

if i dont have 0,255,27 insted i take 0,255,0 it will not have a difrent colloer it will be green

loud salmon
boreal sand
#

yea ik i am saying u didnt make a mistake

#

collor code 0,255,27 is lime green

loud salmon
boreal sand
#

so here it should be lime green when i get 30 hp but i get cyan BUT if i change the coller from 0,255,27 to 0,255,0 then it will not turn cyan collor on 30 hp it will be the CORRECT COLLOR green```cs
private void HealthStage(int StageHP, Color stageColor)
{
if (HP == StageHP)
{
m_NewColor = stageColor;
m_SpriteRenderer.color = m_NewColor;
}
}

public void UpdateColor ()
{
HealthStage(30, new Color(0,255,27));
HealthStage(20, new Color(249,255,0));
HealthStage(10, new Color(255,0,0));
}

#

makes sense?

loud salmon
#

No lol

boreal sand
#

unity is change the collor of the sprite on 30 hp

loud salmon
#

Yes

boreal sand
#

it should cahnge to lime green with 0,255,27 but gives cyan

loud salmon
#

And cyan is one of the other HealthStages or?

boreal sand
#

but if i do 0,255,0 unity will not make it cyan it will be corect collo

boreal sand
loud salmon
#

I mean it's RGB so 0,225,0 is pure green

loud salmon
boreal sand
#

On 30 Hp should be lime green its cyan ( Discord taking a crap with img right now so phone img it is)

#

its not the right collor code

loud salmon
#

I don't really know why it gives the wrong color

boreal sand
#

its ok i just pick the 0 255 0 then it gives corect collor

#

litle weird

loud salmon
#

Yeah but I've no idea why

boreal sand
#

2 question if i want to make it so hp dosnt go above 30 do i do it in the

loud salmon
#

There are several ways you can do it

#

If you use a Slider to get the values it'll automatically be capped

#

You could also use a property

boreal sand
#
if (hp == 30)
{
hp = 30;
}
``` this was an idea but idk if it work or is efective
boreal sand
loud salmon
boreal sand
#

only one that gets health

loud salmon
loud salmon
#

Or just > 30

loud salmon
boreal sand
#

i love the slider on earth but its not enugh space in the game with 2 πŸ˜… thats why i dont have 2 sliders or else i do that why insted my player is the "slider" aka changing collor

loud salmon
#

Oh no I mean, that you can have a slider, without it being visually displayed

#

You just gain functionality of the slider which can be of use

loud salmon
#

Is probably the easiest

boreal sand
loud salmon
#

Well

#

You'd have you private int health

#

as a 'backer field'

#

You then make a property, called Health, which has a get and a set

#

the get will just be the beckerfield

#

so get => health

#

And your set will be your backerfield, so again, health, but clamped

#

So whenever it gets set, it will refer to the property, which then runs the code, which if it exceeds your limit of 30, it will stay 30

loud salmon
#

As example

boreal sand
#

and tha value?

#

30`? since my player has 30 as max health

loud salmon
#

yes

#

You'd have Mathf.Clamp(value, 0, 30)

#

Assuming you want 0 as minimum

boreal sand
#

0 it will destroy gamobject

loud salmon
#

but if it's -1 before 0 will it still destroy the object?

boreal sand
#

idk never will be -1

loud salmon
#

You never know

boreal sand
#

dies on 0

loud salmon
#

It's always safe for the extra measurement

#

Mathf.Clamp needs a minimum anyway, so just set it to 0

boreal sand
#
private static int HPtest = 30;
public static int HP;
{
get => HPtest;
set => HPtest =value.clamp(value, 0, 30);
} ```
#

wpuld that be corect?

#

or am i so far frong

loud salmon
#

Yes except the backingfield naming is terrible

#

Also, HPtest = 30 is unnecesarry

#

Just, HPtest

#

Also, don't make it static

#

That's just an example

#

And the HP shouldn't be static either and can be private

#

I had weird modifiers for it due to unit tests

boreal sand
#

ooh cuss i already have somthing called public int hp = 30

#

so can i just use that name???

loud salmon
#

I'd just call the backing field _health
and the property Health

boreal sand
#

but how does health comunicate with hp?

loud salmon
#

what

boreal sand
#

Anothe phone img keep kot working on pc

#

How does what script u made work with this if i dont write anything for it

loud salmon
#
private int _health;
private int Health;
{
get => _health;
set => _health = Mathf.Clamp(value, 0, 30);
} 
#

This

#

And in your code, you adjust Health

#

You will never touch _health

#

_health is just the backing field for the property

#

It only gets used for the property, and by nothing else

boreal sand
#

that i understand

#

but i dont understadn how that says to the my void getHP that 30 is max

#

if thay dont have same varialbe name

loud salmon
#

If your function says Health += GiveHPToPlayer then it works

#

Your property, Health, makes sure it doesn't go above 30

boreal sand
#

but i cant cuss i made it to say hp is 30

loud salmon
#

what

boreal sand
#

or do i delete the hp = 30

loud salmon
#

Your HP variable would become Health

#

Which is the property

#

You can also keep the same name Ig

#

But then your backing field should be _HP or smth

boreal sand
#

but do i delete the hp = 30 then

#

and type private int hp;

loud salmon
#

It'll work if you just throw this under it

{
get => _health;
set => _health = Mathf.Clamp(value, 0, 30);
} 
#

Given you've a backing field called _health

#

Though, you've to remove the = 30

boreal sand
#

but if i name that health i have to change every name with hp in in this script no?

loud salmon
#

what?

boreal sand
#

omg my enlgish

#

4 am

#

english

loud salmon
#

Maybe you should go to sleep heh

boreal sand
#

updated above

#

no i cant just want to finish hp to day πŸ™‚

loud salmon
#

I told you before that you can rename things

#

So pressing F2 on the variable will rename it

#

Given it's selected

boreal sand
#

ik i just dont liek cahnging names

loud salmon
#

You literally press one button and then you proceed to rename one variable, which renames all occurences

loud salmon
boreal sand
#

Why didnt work ?

loud salmon
#

Because errors

#

But your IDE isn't configured so you cannot see it

#

The first thing you should tomorrow is configure your IDE

boreal sand
#

I have4 errors

loud salmon
#

But, you forgot the = and mathf is Mathf

boreal sand
#

Pc crashed πŸ₯²

#

From error

loud salmon
#

What now

#

How

boreal sand
#

Honestly idk

loud salmon
#

interesting

boreal sand
#

Ok i do the Max Hp tomorrow i cant think straight

#

And ide

loud salmon
#

Fair enough

#

I'm of to watch a show with me parents then

#

Goodnight chap!

boreal sand
#

Goodnight please dont remove this tread need for tomorrow.

loud salmon
#

Did you succeed?

boreal sand
#

now i am makinga menu scene

loud salmon
#

Installed or configured

#

There's a difference

boreal sand
#

both

loud salmon
#

Good

boreal sand
#

yes

loud salmon
#

And it also suggests Unity Methods?

boreal sand
#

dosnt show green though only red

boreal sand
loud salmon
loud salmon
#

Noice

boreal sand
#

making a scene that makes it so the game dont start right away but u have a mane menu scene

#

after that its only cleaning up code

#

πŸ™‚ thanks again

#

very much help

loud salmon
#

Nice

#

Also, your little space invader thingy is adorable

#

It was also the first thing I had to do

#

Or well, one of the first things

boreal sand
loud salmon
#

Of course I would hah

#

Saw it with first screenshot you sent me, it's an iconic game :p

#

Still was more fan of Snake, but still

boreal sand
#

haha

loud salmon
#

Is it for an assignment actually?

#

Like for school?

#

Or is just your own little practice thingy

boreal sand
#

yea school we could choosee what language aslong as u make your own type of space invaders

#

not a knock of "witch half of class did" -_- they want be graded well if the just copy a wido doing exactly that but not my job to worry

#

i never done c# coding so i wanted a challeng and a challeng it was lets just say that

loud salmon
#

They won't be graded? Really?

boreal sand
#

they will be but a low low grade

loud salmon
#

Oh you mean like it counts for e.g. only 10%

boreal sand
#

because then teacher cant tell if its home made or copt

#

our grading is number 6 is best 1 is worst they would get 2-3

#

just for making exact same space invaders

#

1 is just a fail then u didnt do anything

#

can i still ask u question ?

loud salmon
#

Sure go ahead

#

I'm just chillin makin a combat system

boreal sand
#

lmao

#

looks good

#

the figure i mean

#

no so i am trying to make 2 scenes

#

one for mane menu

#

if i do the ctr n to make new scen i "lose" main scen

#

and cant find it

#

where do i find it

loud salmon
boreal sand
#

lol

loud salmon
#

I've no idea what oyu mean

#

The scenes are just within files

boreal sand
#

ahhh

#

i have to save in scenes

#

first time it did it for me

#

so i thought it do it again

#

mb sry

loud salmon
#

Ah

#

Sorry for the late response btw lol

#

I was taking a break