Variable not affecting spawned in gameObject
I'm kind of new to C# so I probably made a silly mistake, but my variable is not affecting a spawned in enemy. I want the player to be able to upgrade its damage and then be able to do more damage to enemies, however, when i'm playing the game and I upgrade its damage, the damage does increase in the prefab, however, the already spawned in enemies are stuck on the original damage and is not changing. How do I fix this? Please ask if you need more code or have and questions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using TMPro;
public class EnemyDamage : MonoBehaviour
{
public int health = 8;
public Transform target;
private NavMeshAgent enemy;
public Transform bulletSpawnPoint;
public GameObject bulletPrefab;
public float bulletSpeed = 8f;
bool playerIsCloseEnough = true;
float distance;
public GameObject protons;
public GameObject elementDrop;
public ParticleSystem hurtEffect;
public int dropAmount;
public ShootBullet shootBulletScript;
public int playerDamage = 1;
// Start is called before the first frame update
void Start()
{
StartCoroutine(EnemyFire());
}
void Awake()
{
enemy = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
distance = Vector3.Distance(target.transform.position, transform.position);
if(distance <= 30)
{
playerIsCloseEnough = true;
}
}
IEnumerator EnemyFire()
{
while(playerIsCloseEnough)
{
if(playerIsCloseEnough)
{
target = GameObject.FindGameObjectWithTag("Player").transform;
GetComponent<NavMeshAgent>().destination = target.position;
var bullet = Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation);
bullet.GetComponent<Rigidbody>().velocity = bulletSpawnPoint.forward * bulletSpeed;
yield return new WaitForSeconds(0.8f);
}
}
}
void OnTriggerEnter(Collider obj)
{
if(obj.gameObject.tag == "Player")
{
Instantiate(hurtEffect, transform.position, transform.rotation);
health = health - playerDamage;
if(health <= 0)
{
Destroy(gameObject);
for (int i = 0; i < dropAmount; i++)
{
Instantiate(protons, transform.position, transform.rotation);
}
Instantiate(elementDrop, transform.position, transform.rotation);
}
}
if(obj.gameObject.tag == "Bullet")
{
Instantiate(hurtEffect, transform.position, transform.rotation);
health = health - playerDamage;
if(health <= 0)
{
Destroy(gameObject);
for (int i = 0; i < dropAmount; i++)
{
Instantiate(protons, transform.position, transform.rotation);
}
Instantiate(elementDrop, transform.position, transform.rotation);
}
}
}
}
Your answer
Follow this Question
Related Questions
Let Player move from B5 to D3 like on a Chessboard 0 Answers
I need help with Deleting and Initiating a gameobject 1 Answer
NullReferenceException: Object reference not set to an instance of an object error ? 1 Answer
Lean touch pinch ignore area to pinch 0 Answers
Problem with UI GraphicRaycasting 1 Answer