Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
float bulletsDistance;
[SerializeField] Rigidbody2D bulletPrefab;
[SerializeField] float bulletSpeed = 15f;
enemyController enemyController;
float scaleX;
public void Initialize(enemyController enemyController)
{
// Procurar o objeto do jogador no cenário e atribuir seu transform
this.Player = GameObject.FindGameObjectWithTag("Player").transform;
// Procurar o objeto do inimigo no cenário e atribuir o componente enemyController
this.enemyController = GameObject.FindGameObjectWithTag("Enemy").GetComponent();
this.bulletTransform = GameObject.FindGameObjectWithTag("BulletTransform").transform;
}
void Start()
{
bulletsDistance = 0.2f;
// Debug.Log(enemyController);
}
void Update()
{
Destroy(gameObject, bulletsDistance);
}
private void OnCollisionEnter2D(Collision2D collision) {
if(enemyController != null) {
if(collision.collider.tag == "Enemy") {
Debug.Log(enemyController.enemyHealth);
enemyController.enemyHealth--;
Destroy(gameObject);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -my-script