
It looks like you're new here. If you want to get involved, click one of these buttons!
I am trying to power-up my fireball when I collect a potion, the code seems to be failing when I am referencing the fireball script to access the 'damage' int in order to apply the multiplier. The damage stat is part of a script that's attached to my 'bullet' prefab which is the name of the fireball.
Here is the code that's going wrong along with the console error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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);
}
}
Here is the error from the console in Unity
NullReferenceException: Object reference not set to an instance of an object
PowerUpItem.Pickup (UnityEngine.Collider2D player) (at Assets/Scripts/PowerUpItem.cs:28)
PowerUpItem.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PowerUpItem.cs:16)
I know it's essentially not referencing the right stat but I just don't know how to resolve it! Any help will be appreciated.
Answers
Is the "Fire" component on the Player, or the Projectile?
It’s on the projectile prefab. Which is why I’m stuck 😅
Ok, so what does "Fire" do?
Does it make sense to be on the projectile, or can you get the same results if it's on the player?
Fire is essentially the script for the fireball projectile prefab so it is called when the fireball is instantiated and holds the animation/ speed/ damage components for the fireball projectiles. Essentially no it can’t be on the player :(
Why not?
Can a fireball projectile exist without a player? or some other entity spawning it?
Could the player have something like "ProjectileStats" and store all the stats for all the projectiles in there?
Then, when you spawn a new projectile, you can populate it with stats from the player.
Ooh possibly! Although I’ve no idea how I’d organise that... definitely worth pursuing though.