Forum begins after the advertisement:
[Part 15] Error with changing Weapon’s parent class
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 15] Error with changing Weapon’s parent class
- This topic has 7 replies, 2 voices, and was last updated 2 months, 3 weeks ago by Terence.
-
AuthorPosts
-
September 8, 2024 at 11:19 pm #15785::
I’m watching part 15 right now, and it seems like I’m experiencing an issue around 47:47. It’s the section when they change the MonoBehavior in the Weapon with an Item, and there’s an error. I’ve tried modifying it, but the error still remains.
September 9, 2024 at 11:14 pm #15801::Hi Yuu, can you post the entire script here? I’ll need to have a look at the part your screenshot blocked to see what the error may be.
September 9, 2024 at 11:35 pm #15803::The error line has //The Error line comment on it
Weapon.cs
using UnityEngine;
public abstract class Weapon : Item { [System.Serializable] public struct Stats { public string name; public string description;
[Header("Visuals")] public Projectile projectilePrefab; public ParticleSystem hitEffect; public Rect spawnVariance; [Header("Values")] public float lifespan; public float damage; public float damageVariance; public float area; public float speed; public float cooldown; public float projectileInterval; public float knockback; public int number; public int piercing; public int 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.projectilePrefab = s2.projectilePrefab ?? s1.projectilePrefab; //result.auraPrefab = s2.auraPrefab ?? s1.auraPrefab; result.hitEffect = s2.hitEffect == null ? s1.hitEffect : s2.hitEffect; result.spawnVariance = s2.spawnVariance; 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.knockback = s1.knockback + s2.knockback; return result; } public float GetDamage() { return damage + Random.Range(0, damageVariance); } } protected Stats currentStats; public WeaponData data; protected float currentCooldown; protected PlayerMovement movement; public virtual void Initialise(WeaponData data) { base.Initialise(data); <strong>//The Error Line</strong> this.data = data; currentStats = data.baseStats; movement = GetComponentInParent<PlayerMovement>(); 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()) { Debug.LogWarning(string.Format("Cannot level up {0} to Level {1}, max level of {2} already reached.", name, currentLevel, data.maxLevel)); return false; } currentStats += data.GetLevelData(++currentLevel); return true; } public virtual bool CanAttack() { return currentCooldown <= 0; } protected virtual bool Attack(int attackCount = 1) { if (CanAttack()) { currentCooldown += currentStats.cooldown; return true; } return false; } public virtual float GetDamage() { return currentStats.GetDamage() * owner.CurrentCrit; } public virtual Stats GetStats() { return currentStats; }
}
Item.cs
using System.Collections.Generic; using UnityEngine;
public abstract class Item : MonoBehaviour { public int currentLevel = 1, maxLevel = 1; protected PlayerStats owner;
public virtual void Initialise(ItemData data) { maxLevel = data.maxLevel; owner = FindObjectOfType<PlayerStats>(); } public virtual bool CanLevelUp() { return currentLevel <= maxLevel; } public virtual bool DoLevelUp() { return true; } public virtual void OnEquip() { } public virtual void OnUnequip() { }
}
ItemData.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ItemData : ScriptableObject { public Sprite icon; public int maxLevel; }
September 9, 2024 at 11:40 pm #15804::When I copy the code Passive.cs with the same implementation in the video and in the Weapon.cs, it works perfectly fine but not in Weapon.cs.
September 9, 2024 at 11:40 pm #15805September 10, 2024 at 10:59 am #15807::In yout Weapon class, the Initialise function should be overriding the parent one, like this:
public
virtualoverride void Initialise(WeaponItemData data) { base.Initialise(data); this.data = data; currentStats = data.baseStats; movement = GetComponentInParent(); currentCooldown = currentStats.cooldown; } September 10, 2024 at 2:50 pm #15809::I fixed the error. Thank you so much. I have a question. Do you plan on implementing a shop where the player can buy buffs before entering a stage or level?
September 11, 2024 at 4:36 pm #15812::A shop system will be a few months away, but one of our Patrons have managed to do something similar to a shop system.
You can reach out to him here on Discord if you are interested to learn more about it: https://blog.terresquall.com/community/topic/dungeon-crawler/
has upvoted this post. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: