Forum begins after the advertisement:
My projectilePrefab is null when leveling up
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › My projectilePrefab is null when leveling up
- This topic has 4 replies, 2 voices, and was last updated 1 week ago by Terence.
-
AuthorPosts
-
December 13, 2024 at 10:53 pm #16759::
The game initially ran fine, as did the other weapons, but when I leveled up the Knife it caused the projectilePrefab to be null, which confused me because the Aura weapon worked fine when leveled up and so did the other Passive Items.
December 13, 2024 at 10:55 pm #16760::using System.Collections; using System.Collections.Generic; using UnityEngine; public abstract class Weapon : Item { [System.Serializable] public struct Stats { public string name, description; [Header("Visuals")] public Projectile projectilePrefab; public Aura auraPrefab; public ParticleSystem hitEffect; public Rect spawnVariace; [Header("Values")] public float lifespan; public float damage, damageVariance, area, speed, cooldown, projectileInterval, knocback; public int number, piercing, maxInstances; public static Stats operator +(Stats s1 ,Stats s2) { Stats result = new Stats(); result.name = s2.name ?? s1.name; result.description = s2.description ?? s1.description; result.hitEffect = s2.hitEffect == null ? s1.hitEffect : s2.hitEffect; result.spawnVariace = s2.spawnVariace; result.lifespan = s1.lifespan + s2.lifespan; result.damage = s1.damage + s2.damage; result.damageVariance = s1.damageVariance + s2.damageVariance; result.area = s1.area + s2.area; result.speed = s1.speed + s2.speed; result.cooldown = s1.cooldown + s2.cooldown; result.number = s1.number + s2.number; result.piercing = s1.piercing + s2.piercing; result.projectileInterval = s1.projectileInterval + s2.projectileInterval; result.knocback = s1.knocback + s2.knocback; return result; } public float GetDamage() { return damage + Random.Range(0, damageVariance); } } protected Stats currentStats; public WeaponData data; protected float currentCooldown; protected Player playerMovement; public virtual void Initialise(WeaponData data) { base.Initialise(data); this.data = data; currentStats = data.baseStats; playerMovement = GetComponentInParent<Player>(); currentCooldown = currentStats.cooldown; } protected virtual void Awake() { if(data) currentStats = data.baseStats; } protected virtual void Start() { if (data) { Initialise(data); } } protected virtual void Update() { currentCooldown -= Time.deltaTime; if(currentCooldown <= 0f) { Attack(currentStats.number); } } public override bool DoLevelUp() { base.DoLevelUp(); if(!CanLevelUp()) { return false; } currentStats += data.GetLevelData(++currentLevel); return true; } public virtual bool CanAttack() { return currentCooldown <= 0f; } protected virtual bool Attack(int attackCount = 1) { if (CanAttack()) { currentCooldown += currentStats.cooldown; return true; } return false; } public virtual float GetDamage() { return currentStats.GetDamage() * owner.CurrentMight; } public virtual Stats GetStats() { return currentStats; } }
has upvoted this post. December 14, 2024 at 12:08 am #16764::You are missing the line that updates the projectilePrefab when levelling up. This will cause it to be null:
public static Stats operator +(Stats s1 ,Stats s2) { Stats result = new Stats(); result.name = s2.name ?? s1.name; result.description = s2.description ?? s1.description; result.projectilePrefab = s2.projectilePrefab == null ? s1.projectilePrefab : s2.projectilePrefab; result.hitEffect = s2.hitEffect == null ? s1.hitEffect : s2.hitEffect; result.spawnVariace = s2.spawnVariace; result.lifespan = s1.lifespan + s2.lifespan; result.damage = s1.damage + s2.damage; result.damageVariance = s1.damageVariance + s2.damageVariance; result.area = s1.area + s2.area; result.speed = s1.speed + s2.speed; result.cooldown = s1.cooldown + s2.cooldown; result.number = s1.number + s2.number; result.piercing = s1.piercing + s2.piercing; result.projectileInterval = s1.projectileInterval + s2.projectileInterval; result.knocback = s1.knocback + s2.knocback; return result; }
December 14, 2024 at 12:38 am #16765December 14, 2024 at 12:57 am #16766::No problem. Do check out our Byte Gauntlet competition for the Vampire Survivors series as well: https://blog.terresquall.com/community/topic/byte-gauntlet-1-win-a-copy-of-black-myth-wukong/
You stand to win a copy of Black Myth: Wukong, or a few months’ Patron membership.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: