This is what the attack is meant to look like:
This is what the attack looks like when the weapon is saved and loaded (loaded in a different scene):
As you can see, the prefab reference is gone.
I have added Weapon to Types:
As well as the projectile types:
Note the 'Attack' class never shows up in the Types but that's probably because it is abstract?
What exactly i am doing: From my scene 'Loadout', a weapon is selected and saved as such:
Code: Select all
ES3.Save<Weapon>("startingWeapon", startingWeaponChoices[index]);
Code: Select all
Weapon startingWeapon = ES3.Load<Weapon>("startingWeapon");
Any help would be greatly appreciated. Thanks.
Relevant classes/types below:
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Attack : ScriptableObject
{
public abstract bool CanAttack(GameObject attacker);
public abstract void OnAttack(GameObject attacker);
public float AS = 1f;
public bool offCD;
}
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Projectile Attack", menuName = "Attack/Projectile/Default")]
public class Attack_Projectile : Attack
{
public GameObject projectilePrefab;
public float force;
public override bool CanAttack(GameObject attacker)
{
return offCD;
}
public override void OnAttack(GameObject attacker){
offCD = false;
Cooldown(attacker);
Attack(attacker);
}
public void Cooldown(GameObject attacker){
// Check if phantom or not
if(attacker.tag == "Player"){
offCD = false;
attacker.GetComponent<Cooldowns>().WaitAndDo(AS, () => offCD = true);
}
}
public void Attack(GameObject attacker)
{
Vector3 sp = Camera.main.WorldToScreenPoint(attacker.transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
GameObject objectInstance = Instantiate(projectilePrefab,
attacker.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
// Points missile at mouse pos
float rot_z = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
objectInstance.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
objectInstance.GetComponent<Rigidbody2D>().AddForce(dir * force);
}
}
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Weapon", menuName = "Inventory/Weapon")]
public class Weapon : Equipment
{
public Attack attack;
public List<Attack> attacks;
void OnEnable(){
attacks = new List<Attack>();
if(attack!=null){
addAttack(attack);
}
}
public bool CanAttack(GameObject attacker)
{
return true;
// return attack.CanAttack(attacker);
}
public void addAttack(Attack attackToAdd){
attacks.Add(attackToAdd);
}
public void Attack(GameObject attacker)
{
if(attack != null){
if(attack.CanAttack(attacker))
attack.OnAttack(attacker);
}
foreach (var attackInList in attacks)
{
// Debug.Log("Attacking with: " + attackInList.name);
if(attackInList.CanAttack(attacker))
attackInList.OnAttack(attacker);
}
}
}