Here is my error:
NullReferenceException: Object reference not set to an instance of an object
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RhinoRunnerGaming
{
public class weapons : MonoBehaviour
{
#region Variables
public gun[] loadout;
public Transform weaponParent;
public GameObject bulletHolePrefab;
public LayerMask canBeShot;
private int currentIndex;
private GameObject currentWeapon;
#endregion
#region Monobehavior Callbacks
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Equip(0);
}
if (currentWeapon != null)
{
Aim(Input.GetMouseButton(1));
if (Input.GetMouseButtonDown(0))
{
Shoot();
}
}
}
#endregion
#region Private Methods
void Equip(int p_ind)
{
if (currentWeapon != null) Destroy(currentWeapon);
currentIndex = p_ind;
GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
t_newWeapon.transform.localPosition = Vector3.zero;
t_newWeapon.transform.localEulerAngles = Vector3.zero;
currentWeapon = t_newWeapon;
}
void Aim(bool p_isAiming)
{
Transform t_anchor = currentWeapon.transform.Find("anchor");
Transform t_state_ads = currentWeapon.transform.Find("states/ADS");
Transform t_state_hip = currentWeapon.transform.Find("states/hips");
if (p_isAiming)
{
//aim
t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimspeed);
}
else
{
//hip
t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimspeed);
}
}
void Shoot()
{
Transform t_spawn = transform.Find("Cameras/playerCamera");
RaycastHit t_hit = new RaycastHit();
if (Physics.Raycast(t_spawn.position, t_spawn.forward, out t_hit, 1000f, canBeShot))
{
GameObject t_newHole = Instantiate(bulletHolePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
Destroy(t_newHole, 5f);
}
}
#endregion
}
}
can somebody please help me:)