Hi I’m currently working on a homing missile script and need some advice. because this rocket is fired from a turret so I have to use prefab. but if I use it. target is removed from settings of homing missile prefab. can you help me?
My test code:
[SerializeField] Transform Target;
private PlayerController player;
public LevelManager levelManager;
[SerializeField]
GameObject Explode;
// Use this for initialization
void Start()
{
levelManager = FindObjectOfType<LevelManager>();
player = FindObjectOfType<PlayerController>();
Target = GameObject.FindWithTag("Player1");
}
You’re setting Target again in the Start method, so I’m not sure what your issue is. You’re correct that prefabs cannot reference objects in the scene. Therefore you need to establish those relationships another way, and your FindWithTag call is one way to do that. If Target is still null after pressing play, make sure that the Player actually has the Player1 tag set on them. (Edit: And as @EdGunther rightly points out below me, make sure that the type you’re assigning FindWithTag to matches the type returned by the method.)