#instance TargetTransform target the

1 messages · Page 1 of 1 (latest)

humble granite
#

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

azure tendon
#

can i send a ss of my code so i can tell if i understood it?

humble granite
#

send your code properly.

#

!code

south kernelBOT
#
Posting code

📃 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.

azure tendon
#

like this?

humble granite
#

does the object with TestSpawn on it have a NavMeshAgent on it?

azure tendon
#

i deleted a bit of it because iam trying around to get it to work rn

#

yes the prefab has a navmeshagent attached

humble granite
#

but TestSpawn is not on the prefab, is it?

#

it's a completely separate thing

azure tendon
humble granite
#

what on earth is GetComponent<NavMeshAgent>() supposed to find?

humble granite
#

from where?

azure tendon
#

from there

humble granite
#

why does this thing have an agent?

azure tendon
#

or is it wrong

humble granite
#

is your spawner supposed to also be walking around?

azure tendon
#

its the spawner object

humble granite
#

or is this "Test" object the prefab itself?

azure tendon
#

supposed to chase

azure tendon
humble granite
#

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

azure tendon
#

yes they are

humble granite
#

then why is there only one object, named "test"?

azure tendon
#

ok so test is the prefab

humble granite
#

you need to name these things more clearly at the very least

azure tendon
azure tendon
humble granite
#

name the spawned object "enemy" and the spawner "enemy spawner"

#

or something like that

azure tendon
#

the spawnpoint is a seperate object

#

done

azure tendon
humble granite
#

so, the spawn point is now a thing sitting in the scene

#

and it has a reference to the enemy prefab

#

is that correct?

azure tendon
#

i also did a test with adding force to it, they spawner and ran

#

or moved

#

yk what i mean i guess

humble granite
#

i don't really know, no

#

you need to be specific.

humble granite
azure tendon
#

Yes

humble granite
#

show me the inspectors for the two objects now

#

enemy and spawnPoint

azure tendon
#

the script is in a gamemanager

#

turned on ofc

humble granite
#

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

azure tendon
#

right here

humble granite
#

ah, okay.

azure tendon
#

there is the Instantiate coroutine

humble granite
#

so, that Game Manager exists in the scene

#

that means you can give it a reference to the player

humble granite
#

by adding a Transform field to it

humble granite
#

you can then drag the player object into that field to reference it

azure tendon
humble granite
#

then, when the spawn point instantiates an enemy, it can tell the enemy about the player

azure tendon
humble granite
#

that's where the guide linked earlier comes in.

humble granite
#

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.

azure tendon
#

and above public Transform target:;

#

?

humble granite
#

you will need to declare a field on the game manager, yes

#

that you use to reference the player

azure tendon
#

iam going to try it

humble granite
#

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.

azure tendon
#

alright, thanks for your patiency

#

and when i have agent(theprefab).setdestination(whattoputhere)

humble granite
#

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

azure tendon
#

you mean instantiate in the coroutine

humble granite
#

yes.

#

you instantiate there (and then do nothing further)

#

also, you are referencing the prefab as a Transform

humble granite
#

the type should be whatever the type of the enemy class is

#

i forget what it is

azure tendon
#

object?

humble granite
#

what is the component that controls the enemy?

#

I think you previously had one called "Test"

azure tendon
#

TestSpawn

humble granite
#

that's a very confusing name

#

it sounds like it should do spawning

azure tendon
#

the script

azure tendon
humble granite
#

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;

azure tendon
#

renaming it to enemyController

humble granite
#

now, when you instantiate zombie, the function will return an Enemy

#

which you can then set the target on

azure tendon
humble granite
#

Yes. you should capitalize it LikeThis, not likeThis, though.

azure tendon
#

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?

#

IT WORKSSSSSSSSS

#

HAHHHHHHHHHHHHHHHH

humble granite
#

yes, you'll need to call SetDestination to set where the agent goes

azure tendon
#

but my player doesnt move rn but idc

humble granite
#

now, go back to the guide and figure out how that lines up with what you did

azure tendon
#

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

humble granite
#

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"

azure tendon
#

now my player doesnt move?

#

and they all die at the same time

#

the prefabs

humble granite
#

i'd have to see that code that destroys the zombies

azure tendon
#

and block is the prefab

humble granite
#

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

humble granite
#

as in, the original prefab

azure tendon
#

i want a zombie to die after some time

humble granite
#

the instantiated objects are not prefabs.

azure tendon
#

destroy instance?

humble granite
#

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

azure tendon
azure tendon
humble granite
#

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

azure tendon
#

i dragged enemy in the block field

#

already changed like you said

humble granite
#

I don't see a block field anywhere in that screenshot.

azure tendon
humble granite
#

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

azure tendon
#

yes

#

but i have like 5 and they die at the same time

#

before they didnt chase the player but died after and after

humble granite
#

pause the game and look at the individual enemies

#

they should have different killTime values

azure tendon
#

question? my enemys are now called enemy(clone)(clone)(clone)

#

and adding one clone by every clone

#

that fine or not?

humble granite
#

that sounds like you're instantiating the spawned enemy

#

yes, it is

#
            zombie = Instantiate(zombie);
            zombie.player = target;
azure tendon
#

all same killtime

humble granite
#

you're overwriting the zombie each time

#

use a different variable.

#
var spawnedZombie = Instantiate(zombie);
azure tendon
#

ah ok

azure tendon
humble granite
#

well, you tell me

#

you need to tell the newly instantiated enemy about the player

azure tendon
#

works now

azure tendon
#

target

#

now my player just doesnt move lol

#

it did before

#

idk if the new script somewhere overrides the movement

humble granite
azure tendon
humble granite
#

it probably does happen to work

#

but "happens to work" does not mean "correct"

azure tendon
#

changed

#

can you help me why my player doesnt move too maybe?

#

it did before

#

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

azure tendon
#

my joystick keeps being 0

#

no matter vertical or horizontal

azure tendon
#

@humble granite ?

#

it works