#instance TargetTransform target the
1 messages · Page 1 of 1 (latest)
instance is the freshly-created instance. It needs to be told about the target.
TargetTransform is the field that holds the reference to the target
and _target is the actual target
can i send a ss of my code so i can tell if i understood it?
📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.
does the object with TestSpawn on it have a NavMeshAgent on it?
i deleted a bit of it because iam trying around to get it to work rn
yes the prefab has a navmeshagent attached
yes ;-;
what on earth is GetComponent<NavMeshAgent>() supposed to find?
the component?
from where?
why does this thing have an agent?
or is it wrong
is your spawner supposed to also be walking around?
its the spawner object
or is this "Test" object the prefab itself?
supposed to chase
ye it is
you have mixed up the spawner and the things that get spawned
they should be two separate things
unless you want every enemy to spawn even more enemies
yes they are
then why is there only one object, named "test"?
ok so test is the prefab
you need to name these things more clearly at the very least
i did it for myself bcs i tried it sorry
so this is the prefab
name the spawned object "enemy" and the spawner "enemy spawner"
or something like that
enemy and spawnPoint
so, the spawn point is now a thing sitting in the scene
and it has a reference to the enemy prefab
is that correct?
yes, the spawning works
i also did a test with adding force to it, they spawner and ran
or moved
yk what i mean i guess
is this correct?
Yes
I see no reference from the spawn point to the enemy prefab.
I don't understand what you are doing.
your enemy has a reference to itself
right here
ah, okay.
there is the Instantiate coroutine
so, that Game Manager exists in the scene
that means you can give it a reference to the player
yes
by adding a Transform field to it
yes
you can then drag the player object into that field to reference it
correct
Yes
then, when the spawn point instantiates an enemy, it can tell the enemy about the player
https://paste.ofcode.org/P2Jn5Mybs3sCMsS8K6SpxB gamemanager script
and how to do that
that's where the guide linked earlier comes in.
ok
suppose the enemy component has a field called target
and the spawn point component also has a field called target
then...
var enemy = Instantiate(enemyPrefab);
enemy.target = target;
note that, in this case, enemyPrefab isn't just a GameObject. It's the type of the enemy component
That way, you don't have to use GetComponent to get the enemy component out.
you will need to declare a field on the game manager, yes
that you use to reference the player
iam going to try it
the gamemanager?
Yes.
The thing that exists in the scene needs to refer to the player.
That way, it can tell the instantiated enemies about the player.
Again, read the guide. If you don't understand a specific part of it, I can explain it further for you
But you can't just say "I don't get any of it"
I can't do anything with that.
alright, thanks for your patiency
https://paste.ofcode.org/3c2zEreheM2amhSHEqeHc7c error on enemy.target
and when i have agent(theprefab).setdestination(whattoputhere)
you are instantiating enemies in both Start and in the coroutine
you should probably only do it in the latter
you forgot to set a target there
latter = coroutine?
you mean instantiate in the coroutine
yes.
you instantiate there (and then do nothing further)
also, you are referencing the prefab as a Transform
change to
object?
what is the component that controls the enemy?
I think you previously had one called "Test"
TestSpawn
the script
ye ik i did it for myself as a test
in unity, rename the file to "Zombie", then go into the newly renamed file and change the name of the class to "Zombie"
then, instead of doing public Transform zombie; do public Zombie zombie;
renaming it to enemyController
now, when you instantiate zombie, the function will return an Enemy
which you can then set the target on
so if i have the script called enemyController i do public enemyController zombie;
Yes. you should capitalize it LikeThis, not likeThis, though.
alr but it works now
i did zombie.player(in the script player is player and iam the target)
and then = target
and now just setDestination on it or?
https://paste.ofcode.org/gj6p8t4ma5cpKuiBy8dNxJ GameManager
https://paste.ofcode.org/AAYx5rUdmEesRyWMKbsjXq EnemyController
IT WORKSSSSSSSSS
HAHHHHHHHHHHHHHHHH
yes, you'll need to call SetDestination to set where the agent goes
but my player doesnt move rn but idc
now, go back to the guide and figure out how that lines up with what you did
Thanks for helping i often learn codes the way i understand them and dont know how a guide tells me then
Thank you, you were very patient with me 🙂
only problem now they spawn behind each other and all die at the same time
zombie = Instantiate(zombie, spawnPoint);
i thought like that
but now they all the parent of spawnPoint
that parents them to the spawn point.
if you want to spawn them at the spawn point, just set the zombie's position after instantiating it
or use the version of Instantiate that takes a position and rotation
Instantiate(zombie, transform.position, Quaternion.identity)
Quaternion.identity means "no rotation"
ik
alright, thanks
now my player doesnt move?
and they all die at the same time
the prefabs
i'd have to see that code that destroys the zombies
killtime
and block is the prefab
why does the enemy have a reference to its own prefab?
it shouldn't care about the prefab
you also shouldn't try to destroy a prefab
why
as in, the original prefab
i want a zombie to die after some time
the instantiated objects are not prefabs.
destroy instance?
Destroy(gameObject) is fine.
this would destroy the game object the component is on
if block is actually a prefab reference, Unity will throw an error when you try to destroy it
i have no idea what block is here, really
nah it just destroyed all prefabs lol
enemy
that doesn't tell me very much
is it the enemy prefab?
is it an instance of the enemy?
show me how it gets assigned
I don't see a block field anywhere in that screenshot.
yes, i did destroy(gameobject) now
I did just remember that, if a prefab has a reference to something on itself, that will turn into a reference to the instantiated thing
so block would probably have referred to the instantiated object correctly
now, i noticed that you're just subtracting 1f from killTime every update
this means that it will die after 600 frames
yes
but i have like 5 and they die at the same time
before they didnt chase the player but died after and after
pause the game and look at the individual enemies
they should have different killTime values
question? my enemys are now called enemy(clone)(clone)(clone)
and adding one clone by every clone
that fine or not?
that sounds like you're instantiating the spawned enemy
yes, it is
zombie = Instantiate(zombie);
zombie.player = target;
all same killtime
you're overwriting the zombie each time
use a different variable.
var spawnedZombie = Instantiate(zombie);
ah ok
and then spawnedZombie.target or still zombie.target?
works now
zombie.target
target
now my player just doesnt move lol
it did before
idk if the new script somewhere overrides the movement
pay attention. is zombie the newly instnantiated enemy?
no spawnedZombie but zombie.target works too ;-;
changed
can you help me why my player doesnt move too maybe?
it did before
https://paste.ofcode.org/gfc7ygXde7yn52HDKwnsUp playercontroller script
is it a problem if it doesnt have animations
i deleted them before by readding the player :/
have them so thats solved
but still doesnt move
last thing then im done
my joystick keeps being 0
no matter vertical or horizontal