#and how do i do that

1 messages · Page 1 of 1 (latest)

final spire
#

I'm making a thread so this doesn't block up everyone else

wanton chasm
#

alr thanks

final spire
#

Your spawner has a reference to the player

#

You spawn an enemy

#

and then set the player variable of that new enemy to the variable the spawner has

#

and since the spawner and the player are both in the scene, you can drag in the player on the spawner

wanton chasm
#

so i shld put this code that i have on the spawner

#

and not change anything

final spire
#

Spawner needs:

  1. Reference to player in scene
  2. Reference to prefab it wants to spawn

Enemy needs:

  1. Variable that holds the player

Spawner creates enemy from prefab, sets player variable on it

wanton chasm
#

OH WAIT I THINK IK

#

and i drag thatonto the enemy?

#

the spawner already has those

final spire
# wanton chasm

You can't set a variable to itself. Just declare the variable and don't set it to anything yet

wanton chasm
#

like that then?

final spire
#

Now EnemySpawner should spawn in a copy of this Enemy, then set it's player variable

#

Which means player is going to need to be public

wanton chasm
#

@final spire u there?

autumn kestrel
final spire
wanton chasm
final spire
wanton chasm
#

it already has right

final spire
wanton chasm
#

this

#

i got that off yt actually not chatgpt 👍

autumn kestrel
final spire
wanton chasm
final spire
wanton chasm
#

red square enemy???

#

im confused

final spire
final spire
#

Which object is the Enemy variable set to

wanton chasm
final spire
wanton chasm
#

no

#

oh

#

the prefab??

final spire
#

every frame

#

the prefab that doesn't exist yet and cannot move

#

Why?

wanton chasm
#

it does exist

#

it spawns in every second

autumn kestrel
final spire
#

That Update function is telling the Enemy prefab to move

#

Prefabs don't exist in the scene, they can't move

wanton chasm
#

they start existing in the scene once they start spawning in

#

every second

final spire
#

Enemy is a reference to the prefab

#

It doesn't matter how many times you clone the prefab

#

Enemy still references the prefab

#

And you attempt to move Enemy

#

Which is the prefab

#

because you have never set it to anything else

wanton chasm
#

so a prefab clone isnt the same as the prefab?

autumn kestrel
#

you're trying to move the enemy in the spawner script?

wanton chasm
final spire
#

The enemeis should move themselves

#

that's the entire point of them having a player variable

autumn kestrel
#

the enemies should have their own script to handle their logic

wanton chasm
#

put the movement script after the spawn script

final spire
wanton chasm
#

the spawn scriptis in the spawner

autumn kestrel
#

the spawners purpose should be to spawn and initialise the enemies

final spire
wanton chasm
#

then it was smn else

final spire
#

You just copied the script into the spawner script of your own volition

#

No one said to do that

#

I said to have the spawner script set the Player variable on the enemies it spawns

wanton chasm
#

oh idk then

#

this then?

final spire
# wanton chasm
  1. You have two Player variables
  2. Why is this script referencing a different Enemy and trying to move that enemy
wanton chasm
#

isnt it referencing itself?

final spire
wanton chasm
#

well what else do i put into the .transform.position?

final spire
wanton chasm
#

ohhh

#

so if smth is referencing itself ijust remove it completely?

final spire
#

There is no point to having a variable to store a reference to yourself

#

For the same reason you don't put your own phone number in your contacts

#

It is the object. It doesn't ever need to "call" itself

wanton chasm
#

ohh yhyhyh

autumn kestrel
#

yea you dont need references to the gameobject the script is attached to

wanton chasm
#

ok so what next

final spire
wanton chasm
#

does it not already do that?

final spire
#

Where do you set it to anything

wanton chasm
#

in the spawner?

autumn kestrel
#

the example code snippet should be all you need

final spire
wanton chasm
final spire
#

Now where do you set the one on the Enemy

autumn kestrel
#

show me your enemy spawning function

wanton chasm
final spire
#

Which is why I have spent the better part of an hour telling you to set it after you spawn the enemy

wanton chasm
#

yes but how

#

😭

final spire
#

Instantiate returns a reference to the thing it made

#

So you spawn an enemy

#

store that enemy in a variable

#

set that enemy's Player to the spawner's Player

#

and now your freshly spawned enemy has a value for Player

wanton chasm
#

im so confused

autumn kestrel
#

yet again this is why you learn to code first

wanton chasm
#

i tried

final spire
wanton chasm
wanton chasm
#

like

#

int number1

number1 is a variable

final spire
#

they're on two different objects

#

The spawner has a Player variable

#

the enemy has a Player variable

wanton chasm
final spire
#

these two things have absolutely nothing to do with each other at all

autumn kestrel
#

create a local variable for the clone (spawned enemy)

#

like Enemy spawnedEnemy;

#

then instantiate the enemy by doing spawnedEnemy = instantiate blah blah

wanton chasm
#

no idea what instantiate means

final spire
autumn kestrel
#

then you can do spawnedEnemy.Player = Player

wanton chasm
autumn kestrel
final spire
#

Once it's in your code it's yours

final spire
#

you have to understand it

autumn kestrel
wanton chasm
#

i tried my hardest

autumn kestrel
#

what I liked to do when following tutorials/copying code is to copy the code then follow it line by line and make sure I understand what absolutely everything is doing

#

if I don't understand what something does, I check the documentation and read up on it

wanton chasm
final spire
#

You are cloning the prefab. The prefab does not really exist, so you are making a copy of it that does

autumn kestrel
#

its creating an instance of that prefab

final spire
#

Making an instance of the prefab, hence, instantiate

autumn kestrel
#

hivemind behaviour

#

but yea, you wanna

  • create a local variable of type Enemy for the spawned enemy
  • instantiate the enemy and assign it to the local var
  • adjust the variables you need inside the enemy after instantiating it
#

what the fuck bullet points since when

wanton chasm
#

how do i differentiate between a spawned and non spawned enemy?

autumn kestrel
#

what the hell does that even mean

#

if an enemy is non spawned it doesn't exist

#

the gameobject is simply not there

wanton chasm
#

how can you make a variable of something that doesnt exist

#

wait

#

no

#

nvm im stupid

#

thats not what ui siad

autumn kestrel
#

it's a variable but it doesn't have a value

final spire
#

You spawn a copy of it

#

then change the variable on that copy

#

You don't need to keep a reference to that enemy after you set the variable

wanton chasm
#

how do you tell the variable 'yo, ur that copy'

#

when it doesnt exist

autumn kestrel
#

instantiate() has a return value

#

when you instantiate an object, it returns that object

#

therefore, spawnedEnemy = instantiate() will assign whatever object you spawned to spawnedEnemy

wanton chasm
#

this should work then right?

final spire
#

Why is the spawner still trying to move the enemy

autumn kestrel
#

you're doing it wrong

wanton chasm
#

this then

autumn kestrel
#

enemySpawned should be local

#

I mean, TBF it shouldn't matter though

wanton chasm
autumn kestrel
#

local is within a function

#

a variable that only exists within one, and therefore is local to that function

wanton chasm
#

shldnt it share the variable EnemySpawned across scripts tho?

autumn kestrel
#

a global variable on the other hand exists globally inside the class, and is accessible within any function

autumn kestrel
#

any public global variable is accessible by other scripts

#

private cannot be accessed by other scripts

#

readonly public can be accessed but not changed

wanton chasm
#

alr alr

#

i still dont know how to fix this tho 😭

autumn kestrel
#

the enemy script should have a variable that references the player object, you are trying to assign that after you spawn it

#

what type is EnemySpawned?

wanton chasm
#

public gameobject

autumn kestrel
#

can you change it to Enemy?

#

will that spit out errors?

wanton chasm
#

its giving errors anyway

#

but in the script itself no

autumn kestrel
#

in the compiler yea

#

aight then that will be easier

#

after you spawn the enemy, you can simply just do EnemySpawned.Player = Player;

wanton chasm
#

wait

#

i changed enemyspawned to enemy

#

😭

#

what did u say i shld change??

#

to enemy

autumn kestrel
#

enemyspawned's type

#

instead of a gameobject it's enemy

#

so it should be public Enemy EnemySpawned;

wanton chasm
#

ohh

#

yh that works

autumn kestrel
wanton chasm
#

but the enemyspawned doesnt know what player is

autumn kestrel
#

exactly

#

that's why you need to assign it

#

the spawner knows what player is

#

you are assigning the player inside the enemy script to whatever it is inside the spawner script