#Null error
1 messages Β· Page 1 of 1 (latest)
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
Yeah game development be like that sometimes hah
i am just so frustrated :/
Ima just post the codes here btw https://paste.ofcode.org/NrWr8ytcCw8bKuwsuAb8eK this is the bullet taking dmg
https://paste.ofcode.org/TQRGu8Y3xRB7rsAS6EBX5n this is what loses and gain hp (and more)
https://paste.ofcode.org/Dj5ibxpBYe798TAqfLfL7C this it the is trigger thats basicly all u need (rest of my files are other stuff i be back later if u give up its ok i just need a break of 3 hours not finding the problem
Yup, welcome to game development
Just @ me whenever you're done with your break
Have a nice break :p
Sure
maybe is smart to tell u What i am doing
Okay so, on what object is the MoreHP script?
yeh
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
And these fire red bullets?
and if the enemy hits the red it only gets destroyer
I don't get the last one
the enemey is on top of the screen shot they shot "yellow bullets"
Yes I got that
Who shoots the red bullets
the green box to the side
yes
There's your problem
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
Are the purple ones allies then?
thats why in the red bullet scrip (MoreHP) i chek if the BULLET hits the red bullet
Yes, that makes sense
yes
Only your problem is that in that case
i dont want it to be easy to get the healing thats why they aliens can aslo hit it
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?
i am sry u have to elaborate this is 3 day of my first time in unity and c# i usualy do other languages
while u draw lett me just fixe the naming didnt see it before now
Cool
Okay just to recall, which bullet carried the MoreHP script?
Just so I'm sure of it
Alright
here the 1 log u see is the log i got when my bullet hit the red the other 2 is on this picture
Does this help?
Green Circle is the player, firing the blue bullet, Dark green square, is the enemy, firing the red bullet
oh i see
To my understanding, this is what is going wrong
Oh btw, I drew it two times to represent 2 frames
It's not 2 players and 2 enemies
So you understand what I'm trying to say right?
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
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
:)
If the bullet collides with the player
It can get the script
I can draw that out too if you want :D
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
I do not understand what you mean.
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
yup
u are corect
it works if the player hit the red bullet
yea i understood if u look at my picture u see i gain hp
i start at 30 on my player
PAIN
:)
is ther any fix to this?
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
Through a seperate script
That's not attached to your movement
It's attached to your player
again i am sry i am feeling so dumb
the player movment is attached to my play
unless u mean 2 script on player
files*
one health one acually movment
yes
If my unity would actually boot up I'd give you an example lol
NO NVM NVM
kek
this will be pain full π¦ alright i msg u when i am done lmao
Here, my player has 4 scripts
Hah, reworking is part of game development chap ;)
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
ig :/
Here, in this case, my player loses a life, if it collides with anything that is tagged as Obstacle
i just liuke having 1 folder for each object but your way is more "clean" if that makes sens
?
Smth like that?
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
aah ok
The health should be a private int really
mine?
aaah ok
It depends if you've reference it or not
i see
Like, you could technically also write a function to get your health, whilst it still being private:
There are many ways to code things :)
ooooh
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
yikes
i always force close steam and other game engines they so good at that
are u german?
No, but close, Dutch
ah
Nah I'm downloading a game so steam is like nom
Yeah my most recent creation of a health class uses a Singleton as well
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
to advanced yet for me
I know, but it's fun to show
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
if u think i should use it i can try
Alright, first though, how is your health script coming along?
just finish tested and working
Mind showing?
Fair yeah
script or the inspector
Script please
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
Nah it's fine in here because you don't completely blast the global chat lol
Also looks pretty good
So good job
need to start writing the coments on the scripts later π
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]
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:
u mean m_SpriteRenderer.color = m_NewColor; make it to a word then use that insted of same line?
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
wont i have to make stagehp a variable
sry but how does healthstage talk to updatecoloor after the if statment
What do you mean exactly?
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
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
If I didn't want to, I wouldn't be here
I enjoy teaching people stuff
π
Did you manage to implement it?
yea
And do you understand why it works?
i do u explained it well on the private only thing i was "scared of"
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
UpdateColor should be able to be private as wel
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
What do you mean?
but not that its == 30 or ayntihg
HealthStage(30, new Color(0,255,27));
:)
guessing this is the next step?
oh u mean gethp? or the givehptoplayer
Or IncreaseHP
Or HealPlayer
Get sounds like you specifically want to receive the variable
^
i need to add max hp to π but thats easy i just do it later
Lets do the Singleton first, that'll make life easier
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
oh i read in articale i read said
public class Singleton : MonoBehaviour
{
public static Singleton instance;
} ``` is the singleton play hp in this case
Also, what the code does it just, if the Instance is null, the Instance will become this, which is the class you're in.
this should be in the red bullet script no?
And there's an else thhat will destroy the gameobject if there are duplicates
Nope, that'd completely fuck everything :p
Singletons are used for things that only have ONE instance, not more not less.
ahh
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
if (Instance == null)
Instance = this;
``` what does "this"
^
oh
this refers to itself
You can use it for these things as well, because this wouldn't work obviously
But this does
ooo
Also, this is a namespace:
So, if I want to refer to my health in say like my score class
but cant u do that if u write the file name?
You'd need a using PlayerHealth
isnt it the same
?
namespace isnt it the same as what i do here
Here, this works because I've this
calling the file
If I remove it, it won't work
The GetComponent will work the same, but it's not what you want
We are using a Singleton to prevent having to use GetComponent
namespace does the same as that just for singleton
No, namespace is just to limit where you use it
Only classes with using <namespace> can use the class
aaah
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
wait ok u want me to make a namespace what should i call it
ok
PlayerHealth
You usually don't want it exactly as the classname
done and cleaned code up a litle
just spacing and that not all names i do it later so i wont waist your time
lol
if u saw the other files it would be 100 of names calleds testing 1 2 3
to lazy thinking of names
Lol, naming is important though
thats what happend when there is not teacher to teach u how to write a "clean code FROM THE START" first to years
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);
in the moreHP script right?
The OnTriggerEnter2D is supposed to be around it btw my bad
Yes
:D
wait...
?
Debug.Log(Player);
Score.myScore += -10;
Player.Instance.TakeDamage(damage); this is takemdg not give
i cant take dmg when i destroy red bullet
Well, it's for your TakeDamage as well
in red bullet??
ooh
That can also now be reworked
slow brain to day
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
problem
And that is
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
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
Yes but they are two seperate healtbars right?
yes
Oh
Your way of filtering it in the old way worked
But it's just really fkin weird lmao
wait what do u mean
Your old system would filter by if it failed the GetComponent
Which is interesting
Does your Player have a tag?
Give your Player a tag called "Player"
same with earth? or should i lett eath be
No, if you'd do that it'd trigger earth as well
You want
if(hitInfo.CompareTag("Player")
{
}
Around this
Also I'd rename hitInfo to something like other or just collider2D
Well, yeah, it's your local variable of the OnTriggerEnter2D
ok
brb 1 min
(creative name ik ) i named my tag player with smal p that wouldnt mess anyting up right?
Not sure if CompareTag is case sensitive or not
But I'd make tags with a capital
no no the tag is player and whats in the compare tag is also player neither is with Capital p
= they are the same
oh ok
uhm shouldnt i change on trigger hit info and the one on earth
nope dosnt exist then i change all of them
You know you can rename things right?
Also that does mean that your IDE isn't configured
So I'd read the #854851968446365696 for configuring it
i do i just forget
ah
having problem making tag i am writing it but its not getting made jsut give me a sec
Oof
What
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
π
Pretty sure it's just the default Player tag
You can use that
Looks good
This no longer exists
Again, this is important
It'd underline this
If your IDE would be configured
:D
thank you so much
Yw :)
i just have 1-2 more question (very small)
when i gain back to 30 hp it dosnt turn the green coller i said u should
it give this collor
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?
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
I mean the method code
What is in HealthStage
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
Well this was the old old code
Eh?
That's true
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?
No lol
unity is change the collor of the sprite on 30 hp
Yes
it should cahnge to lime green with 0,255,27 but gives cyan
And cyan is one of the other HealthStages or?
but if i do 0,255,0 unity will not make it cyan it will be corect collo
nope 20 hp is yellow 10 hp red
I mean it's RGB so 0,225,0 is pure green
Hm
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
I don't really know why it gives the wrong color
Yeah but I've no idea why
2 question if i want to make it so hp dosnt go above 30 do i do it in the
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
if (hp == 30)
{
hp = 30;
}
``` this was an idea but idk if it work or is efective
only slider on earth not player
only one that gets health
I know but you could make a slider on the player as well if you'd want to
That'd work, it'd just have to be => and not ==
Or just > 30
Or you make a property out of your variable
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
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
oh i see
i dont understand the img though
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
The names I used in my code is just from one of my scripts
As example
0 it will destroy gamobject
but if it's -1 before 0 will it still destroy the object?
idk never will be -1
You never know
dies on 0
It's always safe for the extra measurement
Mathf.Clamp needs a minimum anyway, so just set it to 0
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
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
ooh cuss i already have somthing called public int hp = 30
so can i just use that name???
I'd just call the backing field _health
and the property Health
but how does health comunicate with hp?
what
Anothe phone img keep kot working on pc
How does what script u made work with this if i dont write anything for it
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
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
If your function says Health += GiveHPToPlayer then it works
Your property, Health, makes sure it doesn't go above 30
but i cant cuss i made it to say hp is 30
what
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
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
but if i name that health i have to change every name with hp in in this script no?
what?
Maybe you should go to sleep heh
I told you before that you can rename things
So pressing F2 on the variable will rename it
Given it's selected
ik i just dont liek cahnging names
You literally press one button and then you proceed to rename one variable, which renames all occurences
But if you don't want to rename it just do this under it
Because errors
But your IDE isn't configured so you cannot see it
The first thing you should tomorrow is configure your IDE
I have4 errors
But, you forgot the = and mathf is Mathf
ok
Honestly idk
interesting
Goodnight please dont remove this tread need for tomorrow.
Did you succeed?
oh yea i did and installed ide thank uu alot
now i am makinga menu scene
both
yes
And it also suggests Unity Methods?
dosnt show green though only red
yes but already had that
No worries it's just the IDE having a stroke kek
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
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
glad u notice it was from space invaders
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
haha
Is it for an assignment actually?
Like for school?
Or is just your own little practice thingy
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
They won't be graded? Really?
Fair enough
they will be but a low low grade
Oh you mean like it counts for e.g. only 10%
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 ?
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
I did not design it, we bought an entire pack for like 1,60 euro or smth lol
What?
lol