My fader never fades in when after loading the next scene. Please help. I have spent quite a lot of time on this but I couldn’t pinpoint a problem.
NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.Weapon.Spawn (UnityEngine.Transform rightHandTransform, UnityEngine.Transform leftHandTransform, UnityEngine.Animator animator) (at Assets/Scripts/Combat/Weapon.cs:24)
RPG.Combat.Fighter.EquipWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/Fighter.cs:53)
RPG.Combat.Fighter.RestoreState (System.Object state) (at Assets/Scripts/Combat/Fighter.cs:131)
RPG.Saving.SavableEntity.RestoreState (System.Object state) (at Assets/Scripts/Saving/SavableEntity.cs:42)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/Scripts/Saving/SavingSystem.cs:63)
RPG.Saving.SavingSystem.Load (System.String saveFile) (at Assets/Scripts/Saving/SavingSystem.cs:44)
RPG.SceneManagement.SavingWrapper.Load () (at Assets/Scripts/Saving/SavingWrapper.cs:33)
RPG.SceneManagement.Portal+d__7.MoveNext () (at Assets/Scripts/SceneManagement/Portal.cs:41)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Core;
using System;
using RPG.Saving;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction, ISaveable
{
[SerializeField] float timeBetweenAttacks = 0.5f;
[SerializeField] Transform rightHandTransform = null;
[SerializeField] Transform leftHandTransform = null;
[SerializeField] Weapon defaultWeapon = null;
float timeSinceLastAttack = Mathf.Infinity;
Weapon currentWeapon = null;
Transform target;
Animator animator;
Mover mover;
private void Start()
{
mover = GetComponent<Mover>();
animator = GetComponent<Animator>();
if (currentWeapon == null)
{
EquipWeapon(defaultWeapon);
}
}
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (target.GetComponent<Health>().IsDead()) return;
if (!IsInRange())
{
mover.Moveto(target.position, 1);
}
else
{
mover.Cancel();
AttackBehaviour();
}
}
public void EquipWeapon (Weapon weapon)
{
if (currentWeapon != null) currentWeapon.DestroySpawnedGameObject();
currentWeapon = weapon;
if (currentWeapon == null) return;
currentWeapon.Spawn(rightHandTransform, leftHandTransform, animator);
}
private void AttackBehaviour()
{
transform.LookAt(target);
if (timeSinceLastAttack > timeBetweenAttacks)
{
TriggerAttack();
timeSinceLastAttack = 0f;
}
}
private void TriggerAttack()
{
animator.ResetTrigger("StopAttack");
animator.SetTrigger("Attack");
}
public bool CanAttack(GameObject gameObject)
{
Health health = gameObject.transform.GetComponent<Health>();
return health != null && !health.IsDead();
}
private bool IsInRange()
{
return Vector3.Distance(transform.position, target.position) <= currentWeapon.GetWeaponRange();
}
public void Attack(GameObject gameObject)
{
GetComponent<ActionScheduler>().StartAction(this);
target = gameObject.transform;
}
public void Cancel()
{
StopAttackAnimation();
mover.Cancel();
target = null;
}
private void StopAttackAnimation()
{
animator.ResetTrigger("Attack");
animator.SetTrigger("StopAttack");
}
//animation hit event
public void Hit()
{
if (target == null) return;
if (currentWeapon.HasProjectile())
{
currentWeapon.SpawnProjectile(rightHandTransform, leftHandTransform, target);
}
else
{
print("enemy taking damage");
target.GetComponent<Health>().TakeDamage(currentWeapon.GetWeaponDamage());
}
}
//animation hit event
public void Shoot()
{
Hit();
}
public object CaptureState()
{
return currentWeapon.name;
}
public void RestoreState(object state)
{
string restoredWeaponName = (string)state;
Weapon restoredWeapon = Resources.Load<Weapon>(restoredWeaponName);
currentWeapon = restoredWeapon;
EquipWeapon(restoredWeapon);
}
}
}