So I wanted to make a simple projectile damage on collision script. But since the projectile is a prefab, i cannot drag and assign my player as the target. Thats why i wanted to leave the player gameobject empty and assign it via script. But even though it works, it gives me an error that the player isn't assigned every time the bullet spawn:
private Health health;
public GameObject player;
void Start()
{
if (player == null){
player=GameObject.FindGameObjectWithTag("Player");
}
health=GameObject.FindGameObjectWithTag("Player").GetComponent();
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag==player.tag){
health.DecreaseHealth();
}
Destroy(gameObject);
}
So me not wanting to have an error message, I removed the player gameobject:
private Health health;
void Start()
{
health=GameObject.FindGameObjectWithTag("Player").GetComponent();
}
void OnCollisionEnter2D(Collision2D col){
Debug.Log(player.tag);
if(col.gameObject.tag==GameObject.FindGameObjectWithTag("Player").tag){
health.DecreaseHealth();
}
Destroy(gameObject);
}
and when I do this, it doesen't even create the bullet, which I do in a totally different script.It will only work like this if i put the Debug.Log command there with the player.tag as argument.
Are both of these bugs or am I doing something wrong?,So I was using "public GameObject player" to assign my player, and but when I do, it constantly spams error messages that it isn't assigned, which it isn't in the inspector, but it is in the script in start.
↧