#8-Bit vampire survivors issues
1 messages · Page 1 of 1 (latest)
EnemyStats.cs
https://hastebin.com/share/bebugukusu.csharp
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
EnemyMovement
https://hastebin.com/share/bexinorotu.csharp
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
i accidently fucked the other scripts aswell
if you're using find then you're assuming there's similar objects of that type on the scene already.
so again with the scene. I see nothing on it but your menu.
i still dont get what your trying to say, do you want me to show you the scene in unity?
also heres the playerstats script
https://hastebin.com/share/ofacicociy.csharp
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
and finally the game manager script wich i also ended up fucking up
https://hastebin.com/share/azukuwiwul.csharp
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
at the start this is what the scene looks like, and whenever i start to play the game it spawns enemies every second
ok so you have two scenes and this is the scene that's having problems?
yes
this is the game scene
the other ones are the title screen and character select screen
So let's look at this line:
void Start()
{
player = FindObjectOfType<PlayerStats>().transform;
}
here, you're assuming that at the time this is called, there is a gameobject on the scene that has a component of PlayerStats
What exists on the scene when you load it and what is dynamically instantiated
enemies and whatever weapon the player begins with
so both the players and enemies are on the scene as soon as you load it
yes
do you spawn enemies over time at all
May just look into doing a singleton because it could be a few things but if you want to eliminate one of the larger possibilities it's getting the references correctly
i should be using singletons
i could just remove the components and reaply them
and hope that fixes it
a quick fix to see if you can eliminate some errors is to disable the dynamic enemies and then on the inspector expose your references
expose my references?
[SerializeField] Transform player;
This will work for stuff already on the scene, but when you dynamically spawn stuff you need a way to bind the references which is where the singleton comes in handy
If it exists at scene load time then you can bind references using the editor by saying here, this player exists already so let's point to them
so for now turn off the dynamic spawn and try to get rid of errors with this just to confirm these are the only errors
Also don't bind the reference by transform/gameobject
It should be
PlayerStats player;
Reference the fields from the inspector.
If the objects are spawned by a spawner, you can pass the info to the enemy after spawning it.
what is 31
The usual pattern would becs [SerializeField] private PlayerStats player;``````cs var enemy = Instantiate(enemyPrefab); enemy.player = player; enemy.spawner = this;
wdym?
oh, if you've still got that find method, remove it
what do i use instead of the find method?
You set the reference on the inspector
yeah also the player now does get hit but it just doesnt stop at 0 for some reason
and also the text isnt updating in the pause screen
Were you able to fix the error?
it seems like it, i dunno what i did but now the HP goes down
Are there errors in the console tab?
But yeah stick with that idea instead of the singleton. I just suggested it because it's what you'd want to use over find but in this situation you don't actually need it.
Not referring to unwanted/undefine behavior.
yeah whenever the player colides it returns the error with the currentHP
also the HP bar doesnt update
if you're still using find it's probably removing the reference from the editor
so go around your scripts and bind them directly
and remove find
what do i replace it with?
you replace it with what you're doing with the editor

so i went down a bit on the error logs and im also getting these
GameManager.instance.currentHealthDisplay.text = "Health: " + currentHealth;
this is the line thats giving the error
ill just remove the real time stat thing, cuz thats whats giving me the errors
public class EnemyStats : MonoBehaviour
{
public EnemyScriptableObject enemyData;
[SerializeField] PlayerStats player;
[SerializeField] EnemySpawner es;
void Start()
{
//don't need to set player anymore
}
private void OnDestroy()
{
//don't need to set spawn
es.OnEnemyKilled();
}
}```
remove the dynamic spawning for now and just try to get an idea how to bind references on the editor
im testing something out
later you will be using dalphat's method, but for references that exist on the same gameobject should be set like this
or through GetComponent but you can ignore that for now
i just checked
if (Vector2.Distance(transform.position, player.position) >= despawnDistance)
{
ReturnEnemy();
}
thats line 36
wait thats on enemy stats
that the error is happening
oh sorry right yeah it's enemy stats
I mentioned up above that when you bind references, you try to do it by the script type and not by the gameobject/transform
reasoning why is it's less ambiguous and that when you start connecting references you already know the type
in this case instead of player being referenced as a transform, we reference it by the PlayerStats script type
PlayerStats script has no idea what a position, but if we inherit from monobehavior then it does
i see
and we know that PlayerStats is a monobehavior, meaning that it exists by being wrapped in a gameobject which gives it the ability to be used on the scene
and gameobjects always imply some sort of transform
since you inherit from mono, we've got those references too, and to do that you need to use the dot operator to access those references
i see
player.transform.position
PlayerStats inherits from mono, mono has references to transform
nice, this way of binding allows you to just set a lot of the work at editor time
once you start spawning stuff then you need to start coding the reference fetching in
dynamically
Ideally, if it's a single gameobject with multiple components (object instances) that need to reference each other, then you should do that on the editor
if one game object needs to reference another, then it depends what exists on the scene when it's loaded.
ok new issue, the level up screen doesnt work, like the buttons dont work
should disable that and resolve the spawning right now
Refer to this here for the spawner
theres one thing i could try to do
i could see if i got a backup of the code before i fucked up yesterday
if you were using Find with that too then I'd wouldnt bother*
yeah but before i fucked up the code i was still using find and it was still working
discard any knowledge of that method
Even if it were working, you're bound to run into problems eventually
it's a lazy hack to find references and should only be used for debugging purposes
well
i kinda dont know what else to use
so ima just keep using find atleast on this project
All good. Do consider reading more into it here: https://unity.huh.how/references
ok so unrelated to any of what we talked about here but rn the game is modyfing values in a weird way
for example whenever i decide to get a health passive item
it doesnt increase it from 100 to 150 or 200
and thats what i basicly want it to do
it does this instead
the same happens with every other stat
start throwing out some debug.log() around your values
and just follow all the operations you do on your values
i mean theres 1 equation i did earlier that worked but it increased the HP value by 150