#8-Bit vampire survivors issues

1 messages · Page 1 of 1 (latest)

sour geyser
#

ima dump all the scripts i was working on yesterday that caused the issuse thats happening

#

i accidently fucked the other scripts aswell

white yew
#

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.

sour geyser
#

Screwmeman i still dont get what your trying to say, do you want me to show you the scene in unity?

#

at the start this is what the scene looks like, and whenever i start to play the game it spawns enemies every second

white yew
#

ok so you have two scenes and this is the scene that's having problems?

sour geyser
#

yes

#

this is the game scene

#

the other ones are the title screen and character select screen

white yew
#

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

sour geyser
#

yeah the player

#

wich he does have the component

white yew
#

What exists on the scene when you load it and what is dynamically instantiated

sour geyser
#

enemies and whatever weapon the player begins with

white yew
#

so both the players and enemies are on the scene as soon as you load it

sour geyser
#

yes

white yew
#

do you spawn enemies over time at all

sour geyser
#

yes

#

every second up to 50 enemies spawn

#

and every minute that happens again

white yew
#

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

sour geyser
#

i should be using singletons

#

i could just remove the components and reaply them

#

and hope that fixes it

white yew
#

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

sour geyser
#

expose my references?

white yew
#
[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;
rancid mulch
sour geyser
white yew
#

what is 31

rancid mulch
#

The usual pattern would becs [SerializeField] private PlayerStats player;``````cs var enemy = Instantiate(enemyPrefab); enemy.player = player; enemy.spawner = this;

sour geyser
white yew
#

oh, if you've still got that find method, remove it

sour geyser
#

what do i use instead of the find method?

white yew
#

You set the reference on the inspector

sour geyser
#

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

rancid mulch
#

Were you able to fix the error?

sour geyser
#

it seems like it, i dunno what i did but now the HP goes down

rancid mulch
#

Are there errors in the console tab?

white yew
rancid mulch
#

Not referring to unwanted/undefine behavior.

sour geyser
#

yeah whenever the player colides it returns the error with the currentHP

#

also the HP bar doesnt update

rancid mulch
#

Well, you've got errors.

#

Simply expect nothing to work properly

white yew
#

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

sour geyser
#

what do i replace it with?

white yew
#

you replace it with what you're doing with the editor

sour geyser
#

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

white yew
#
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

sour geyser
#

im testing something out

white yew
#

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

sour geyser
#

alright

#

ok so i changed the enemy stats script now it gives me this error

white yew
#

seems like a whole new error

#

that's on playerstats

sour geyser
#

IAmNowConfusedCrap 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

white yew
#

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

sour geyser
#

i see

white yew
#

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

sour geyser
#

i see

white yew
#

player.transform.position

#

PlayerStats inherits from mono, mono has references to transform

sour geyser
#

alright no errors

#

ok so now the only issue is the HP

white yew
#

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.

sour geyser
#

ok new issue, the level up screen doesnt work, like the buttons dont work

white yew
#

should disable that and resolve the spawning right now

white yew
sour geyser
#

theres one thing i could try to do

#

i could see if i got a backup of the code before i fucked up yesterday

white yew
#

if you were using Find with that too then I'd wouldnt bother*

sour geyser
#

yeah but before i fucked up the code i was still using find and it was still working

white yew
#

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

sour geyser
#

well

#

i kinda dont know what else to use

#

so ima just keep using find atleast on this project

white yew
sour geyser
#

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

white yew
#

start throwing out some debug.log() around your values

#

and just follow all the operations you do on your values

sour geyser
#

i mean theres 1 equation i did earlier that worked but it increased the HP value by 150