#and how do i do that
1 messages · Page 1 of 1 (latest)
alr thanks
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
Spawner needs:
- Reference to player in scene
- Reference to prefab it wants to spawn
Enemy needs:
- Variable that holds the player
Spawner creates enemy from prefab, sets player variable on it
OH WAIT I THINK IK
and i drag thatonto the enemy?
the spawner already has those
like that
You can't set a variable to itself. Just declare the variable and don't set it to anything yet
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
lmao
Yes I'm just waiting for you to do the next thing
bro what😐
What are Enemy and speed1 used for
holup this is off topic personal question is there a way to set the values of variables in a class at the same time you create an instance of that class
So what is Enemy
the enemy
Which enemy
If it's not a mono behaviour you can make a constructor that takes the data. If it is a monobehaviour you just have to set it after you make it
which one
Which object is the Enemy variable set to
the one it spawns
And so you change it every time you spawn a new enemy?
So you are planning on moving the prefab towards the player
every frame
the prefab that doesn't exist yet and cannot move
Why?
well i put it into a namespace so I guess I'm doing it as you should
But you're not doing anything with those
That Update function is telling the Enemy prefab to move
Prefabs don't exist in the scene, they can't move
But that doesn't matter
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
so a prefab clone isnt the same as the prefab?
No
you're trying to move the enemy in the spawner script?
so how do i make it move the clones then?
Why is the spawner moving the enemies at all
The enemeis should move themselves
that's the entire point of them having a player variable
the enemies should have their own script to handle their logic
thats what you told me to do
put the movement script after the spawn script
No it isn't
the spawn scriptis in the spawner
the spawners purpose should be to spawn and initialise the enemies
I never said to do that
then it was smn else
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
- You have two Player variables
- Why is this script referencing a different
Enemyand trying to move that enemy
isnt it referencing itself?
If it's referencing itself why have it at all
well what else do i put into the .transform.position?
transform.position
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
ohh yhyhyh
yea you dont need references to the gameobject the script is attached to
ok so what next
Now set the player variable on your Enemy after you spawn it in the spawner
in the spawner?
Okay, show where you do that
That sets the Player variable on the spawner
Now where do you set the one on the Enemy
show me your enemy spawning function
isnt that impossible as its a prefab
You cannot drag in a scene object into a prefab
Which is why I have spent the better part of an hour telling you to set it after you spawn the enemy
=
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
im so confused
yet again this is why you learn to code first
i tried
You seem to not understand what a variable is
so the var for before spawning and after spawning are 2 differnet variables
i do ido
like
int number1
number1 is a variable
They are two different variables. Regardless of spawning
they're on two different objects
The spawner has a Player variable
the enemy has a Player variable
i dont have a variable for those
these two things have absolutely nothing to do with each other at all
create a local variable for the clone (spawned enemy)
like Enemy spawnedEnemy;
then instantiate the enemy by doing spawnedEnemy = instantiate blah blah
no idea what instantiate means
You are literally using it in your own code
then you can do spawnedEnemy.Player = Player
i didnt write it 😭
bro 😭
Then you shouldn't have put it in your code until you figured out what it did
Once it's in your code it's yours
i tried to
you have to understand it
try harder
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
so instantiantetenad is just a hard word for clone
Basically
You are cloning the prefab. The prefab does not really exist, so you are making a copy of it that does
its creating an instance of that prefab
Making an instance of the prefab, hence, instantiate
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
how do i differentiate between a spawned and non spawned enemy?
what the hell does that even mean
if an enemy is non spawned it doesn't exist
the gameobject is simply not there
how can you make a variable of something that doesnt exist
wait
no
nvm im stupid
thats not what ui siad
it's a variable but it doesn't have a value
EnemyPrefab is a variable that holds a reference to the prefab. This does not change
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
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
Why is the spawner still trying to move the enemy
you're doing it wrong
idk what local or public means
local is within a function
a variable that only exists within one, and therefore is local to that function
shldnt it share the variable EnemySpawned across scripts tho?
a global variable on the other hand exists globally inside the class, and is accessible within any function
if it's local, no
any public global variable is accessible by other scripts
private cannot be accessed by other scripts
readonly public can be accessed but not changed
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?
public gameobject
in the compiler yea
aight then that will be easier
after you spawn the enemy, you can simply just do EnemySpawned.Player = Player;
wait
i changed enemyspawned to enemy
😭
what did u say i shld change??
to enemy
enemyspawned's type
instead of a gameobject it's enemy
so it should be public Enemy EnemySpawned;
then you do this after you instantiate
but the enemyspawned doesnt know what player is