Forum begins after the advertisement:
[Part 15] Cannot convert from WeaponData to ItemData
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 15] Cannot convert from WeaponData to ItemData
- This topic has 3 replies, 3 voices, and was last updated 1 week, 5 days ago by
Alp Apustaja.
-
AuthorPosts
-
June 24, 2025 at 3:39 pm #18344::
using UnityEditor.Experimental.GraphView; using UnityEngine; public abstract class Weapon: Item { [System.Serializable] public struct Stats { public string name, description; [Header("Visual")] public Projectile projectilePrefab; //public Aura auraPrefab; public ParticleSystem hitEffect; public Rect spawnVariance; [Header("Value")] public float lifespan; public float damage, damageVariance, area, speed, cooldown, projectileInterval, knockback; 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.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); this.data = data; currentStats = data.baseStats; movement = GetComponent & lt; PlayerMovement & gt; (); 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 & lt; = 0 f) { Attack(currentStats.number); } } public override bool DoLevelUp() { base.DoLevelUp(); if (!CanLevelUp()) { Debug.LogWarning(string.Format("cannot level up 0 to level 1, max level 2 already reached", name, currentLevel, data.maxLevel)); } currentStats += data.GetLevelData(++currentLevel); return true; } //check the weapon cooldown is up or not public virtual bool CanAttack() { return currentCooldown & lt; = 0; } protected virtual bool Attack(int attackCount = 1) { if (CanAttack()) { currentCooldown += currentStats.cooldown; return true; } return false; } //get the amount of damage that the weapon supposed to deal and factoring the weapon stat (include damage varaiance, characeter might) for a buff damage public virtual float GetDamage() { return currentStats.GetDamage() * owner.CurrentMight; } public virtual Stats GetStats() { return currentStats; } }
Argument 1: cannot convert from ‘WeaponData’ to ‘ItemData’ Why do i get this error and how to fix?
June 24, 2025 at 3:45 pm #18345::Based on the reference information, you need to cast the WeaponData to ItemData when calling base.Initialise(). Update the Initialise method to:
public virtual void Initialise(WeaponData data) { base.Initialise((ItemData)data); this.data = data; currentStats = data.baseStats; movement = GetComponent(); currentCooldown = currentStats.cooldown; }
This explicitly casts the WeaponData to ItemData when calling the base class’s Initialise method, which should resolve the type conversion error.
has upvoted this post. June 24, 2025 at 4:02 pm #18350::Still not working by using the code
public virtual void Initialise(WeaponData data) { base.Initialise((ItemData)data); //Cannot convert type 'WeaponData' to 'ItemData'// this.data = data; currentStats = data.baseStats; movement = GetComponent<PlayerMovement>(); currentCooldown = currentStats.cooldown; }
has upvoted this post. June 24, 2025 at 4:06 pm #18351::The error message indicates that
WeaponData
cannot be directly converted toItemData
. This suggests thatWeaponData
is not a subclass ofItemData
, or there is no implicit conversion defined between them.To fix this issue, you need to ensure that
WeaponData
inherits fromItemData
. Here’s how you can do it:-
Check the Inheritance: Make sure that your
WeaponData
class is defined to inherit fromItemData
. It should look something like this:public class WeaponData : ItemData { public Stats baseStats; // Assuming baseStats is defined in WeaponData // Other properties }
-
Modify the Initialise Method: After ensuring that
WeaponData
inherits fromItemData
, yourInitialise
method can stay as is:public virtual void Initialise(WeaponData data) { base.Initialise(data); // This should now work if WeaponData inherits from ItemData this.data = data; currentStats = data.baseStats; movement = GetComponent(); currentCooldown = currentStats.cooldown; }
-
Check for Compilation Errors: Make sure there are no other compilation errors in your project that may be interfering with the recognition of the inheritance.
If
WeaponData
cannot or should not inherit fromItemData
, you will need to rethink your class structure and how you manage the data for weapons and items. In that case, consider creating a common interface or base class that bothItemData
andWeaponData
can inherit from.Let me know if you need further assistance!
has upvoted this post. June 24, 2025 at 4:24 pm #18352June 24, 2025 at 4:27 pm #18353::Great! Could you share the specific solution you used to fix the problem? This will help me understand how you resolved the type conversion issue and potentially help other developers facing similar challenges.
Would you be willing to provide more details about the fix you implemented?
has upvoted this post. June 25, 2025 at 1:52 am #18367June 25, 2025 at 1:55 am #18369::Thank you for the kind words! However, the reference information does not provide details about the specific solution to the type conversion issue.
Could you share more details about how you fixed the problem with the WeaponData and ItemData type conversion?
-
-
AuthorPosts
- You must be logged in to reply to this topic.