Hi, I was just wondering how I would search for a public transform in my script attached to my instatiated enemy prefab?
public Transform targetFront;
Would I use FindGameObject or GetComponent or another one?
Thank you in advance
Hi, I was just wondering how I would search for a public transform in my script attached to my instatiated enemy prefab?
public Transform targetFront;
Would I use FindGameObject or GetComponent or another one?
Thank you in advance
More context is needed. Are you trying to assign to this field? Do you want some other component to be able to get this reference?
Describe your end goal here.
Hey sorry, yes so i would like to assign this field once the enemy has been spawned in.
So I can usually attach this object to the enemy, but now it is a prefab i cant assign it
So i need to attach it in script and since its not a Public GameObject, i’m not sure how to to do
I hope that helps, i can rephrase it if it still does not make sense
Thanks
So is targetFront what this enemy is targeting? Such as the player or something?
If it’s meant to find the player, I’d set up a singleton pattern on your player so other scripts can easily find it’s location.
I have a target front and a target back, it is essentially a crude way for me decide which side of the player to attack when it collides
I’m still genuinely confused. Is this object on the same game object or a child game object of the enemy? Or is it on another game object?
The target front and back objects are attached to the player
The “public transform target front and back” are in my enemy script, so i would usually drag those objects to the enemy in the inspector, but now trying to find it through the enemy script once the enemy has spawned
Then yeah, give your player some kind of singleton pattern instance that can allow other objects access to it’s properties.
If you don’t know what that means, there’s a million tutorials on the pattern out there.
Thank you for your help, I think i’ve solved my issue by adding a tag to the object and using
targetFront = transform;
GameObject front = GameObject.FindGameObjectWithTag("front");
targetFront = front.transform;
Not sure if this would cause other issues but seems to be working
In general it’s best to avoid using .Find and any similar methods. They are not performant, and become a crutch that if you lean on too often, will make designing more involved systems impossible.
I strongly suggest looking up an learning some fundamental patterns like the singleton pattern. It will be of much more use to you than relying on .Find and similar.
I will take a look another time, thank you for all the help though, much appreciated