- Home /
Power-Ups increasing projectile stats
I am trying to increase the damage of the players fireball when a power up is picked up. I am getting an object reference error, I'm pretty sure this is due to the code being linked tot he player for the collision and the fireballs have their own script detailing their speed, damage etc.
Essentially I need a way to reference my fireball prefab (Called Bullet) so the code can access the 'damage' stat and increase it for a period of time.
The code for the power-up is:-
public class PowerUpItem : MonoBehaviour
{
public GameObject pickupEffect;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Pickup(other);
}
}
void Pickup(Collider2D player)
{
Instantiate(pickupEffect, transform.position, transform.rotation);
Fire stats = GetComponent<Fire>();
stats.damage *= 2;
Destroy(gameObject);
}
}
The code for the fireball prefabs (known as Fire) is:-
public class Fire : MonoBehaviour
{
public Animator animator;
public float speed = 20f;
public int damage = 30;
public Rigidbody2D rb;
public GameObject FireEmbers;
public float liveTime = 1f;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.right * speed;
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
Enemy enemy = hitInfo.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
Destroy(gameObject);
Instantiate(FireEmbers, transform.position, Quaternion.identity);
}
void Update()
{
liveTime -= Time.deltaTime;
if (liveTime <= 0)
{
Destroy(this.gameObject);
Instantiate(FireEmbers, transform.position, Quaternion.identity);
}
}
}
Answer by Aviryx · Aug 25, 2020 at 03:55 PM
Assuming you didn't forget to post some of your code it looks like your issue is here:
Fire stats = GetComponent<Fire>();
This is looking for a component on the pickup gameobject.. but your Fire.cs script is attached to each spawned bullet, not the pickup gameobject.
The best solution would be to use Scriptable Objects to store data about bullet values (damage, speed etc). This way you could access the data independent of the object itself (ie - you do not need to try and get a reference for each instantiated bullet - you can just access the SO data and change values).
https://docs.unity3d.com/Manual/class-ScriptableObject.html
https://learn.unity.com/tutorial/introduction-to-scriptable-objects
using UnityEngine;
[CreateAssetMenu(fileName = "BulletData", menuName = "Data/Bullet", order = 1)]
public class BulletDataScriptableObject : ScriptableObject
{
public float speed;
public int damage;
}
public class Fire : MonoBehaviour
{
public Animator animator;
BulletDataScriptableObject bulletData;
public float speed;
public int damage;
// ....
// ....
void Start()
{
speed = bulletData.speed;
damage = bulletData.damage;
}
public class PickupExample : MonoBehaviour
{
BulletDataScriptableObject bulletData;
void OnTriggerEnter2D(Collider2D other)
{
bulletData.damage = 100;
}
}
Thank you for this!
I am still super new at this whole program$$anonymous$$g/ game dev thing with essentially 3 weeks experience so I really appreciate the lesson and the examples! :)
I am going to try and implement these now and see if I can do it properly :)
You could also look into static classes. They do not require a reference to access their methods/variables. However, static classes stay in memory so they are not always the best choice. In your example scriptable objects are the best way to go... but it might be worth familiarising yourself with the static modifier (https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/classes-and-structs/static-classes-and-static-class-members)