Hi when hero changes scene ım Instantiating it to another scene and camera follows it via code but ım using
findobjectwithtag to find hero and it gives nullreference why its giving nullreferenceexception
this is herocreator script
public void herocreate()
{
if (main_hero_move.herocreate == 3&& herocreator)
{
Instantiate(heroo, getpos, Quaternion.identity);
herocreator = false;
}
}
this is camera script it gives nullreference on line 28 29 dx dy fields.
public GameObject player;
float dx ,dy;
float x, y,z;
Rigidbody2D rb;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Main_hero");
}
void Start()
{
z = -100;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
// if (player != null)
{
float a, b;
dy = player.transform.position.y - transform.position.y;
dx = player.transform.position.x - transform.position.x;
it works if ı add
player = GameObject.FindGameObjectWithTag(“Main_hero”);
if (player != null){
}
to update function