- Home /
Changing Bullet Damage from another Script
So I'm trying to create power-ups for my game but can't figure out how to change the damage int that's in the projectile script through the power-up script. I'm trying to increase the bullet damage for 5 seconds but all the methods I've tried won't work. When I tried the different methods I didn't get any errors but nothing is changing the int. The script is: public int multiplier = 5; public float duration = 5f; public GameObject DamageUI; public GameObject damageBar;
public GameObject pickupEffect;
public GameObject parent;
void Awake()
{
DamageUI.SetActive(false);
damageBar.SetActive(false);
GetComponent<SpriteRenderer>().enabled = true;
GetComponent<Collider2D>().enabled = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
StartEffects(other);
}
}
void StartEffects(Collider2D other)
{
Debug.Log("Trying to Start Coroutine");
StartCoroutine(Pickup(other));
StartCoroutine(AnimationDelay());
}
IEnumerator Pickup(Collider2D player)
{
DamageUI.SetActive(true);
damageBar.SetActive(true);
Instantiate(pickupEffect, transform.position, transform.rotation);
// CHANGE BULLET DAMAGE TO *= MULTIPLIER
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<Collider2D>().enabled = false;
yield return new WaitForSeconds(duration);
// CHANGE BULLET DAMAGE TO /= MULTIPLIER
Destroy(gameObject);
Destroy(parent);
}
IEnumerator AnimationDelay()
{
yield return new WaitForSeconds(5);
DamageUI.SetActive(false);
damageBar.SetActive(false);
}
I'm trying to change the int that's attached to the projectile that is instantiating everytime I click, here's the code that I'm using:
[Header("Bullet Stats")]
public float speed;
public int damage;
public float lifeTime;
public float distance;
[Space]
public LayerMask whatIsSolid;
public GameObject destroyEffect;
// Use this for initialization
public void Start () {
Invoke("DestroyProjectile", lifeTime);
}
// Update is called once per frame
private void Update ()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if(hitInfo.collider != null)
{
if(hitInfo.collider.CompareTag("Enemy"))
{
Debug.Log("Hit Enemy || Taken damage");
hitInfo.collider.GetComponent<Enemy>().TakeDamage(damage);
}
DestroyProjectile();
}
transform.position += transform.up * speed;
}
void DestroyProjectile()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Someones help would really be apreciated, thank you for your time :) ( I hope that made sense )
i think i might see the issue but very quickly is this all the same script or two separate scripts and are you trying to be able to stack these effects or simply have it active as a singular for a certain amount of time.
Answer by ray2yar · Jan 31, 2019 at 12:39 AM
Well, I won't go through your code and tell you how to implement this... namely because I don't even see the code that instantiates the bullet you're talking about... but as far as changing a value on one script from another that's fairly easy. Here is ONE implementation that does that very thing.
public GameObject MyBullet;
bool IsPoweredUp;
void CreateBullet()
{
GameObject ABullet = Instantiate(MyBullet);
if (IsPoweredUp)
{
ABullet.GetComponent<BulletScript>().PowerUpFunction();
}
}
Another implementation might involve increasing the damage of all bullets, including active ones... so this SECOND implementation does the same thing basically, but should work in conjunction with the one above.... (as this one does not change the damage of future bullets
void PowerUpBullets()
{
foreach(GameObject bullet in GameObject.FindGameObjectsWithTag("Bullets"))
{
bullet.GetComponent<BulletScript>().PowerUpFunction();
}
}
Here's what the function the bullet might look like... you could even pass it a couple variables if you wanted.
public void PowerUpFunction()
{
//do stuff to increase the effect
Invoke("PowerDown", timeLimit);
}
void PowerDown()
{
//do stuff to decrease the effect
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612190811im_/https://answers.unity.com/themes/thub/images/avi.jpg)