My bullet is trying to set the game object “gun” to a game object that doesn’t even exist in my assets what so ever.
Shotgun2 doesn’t even exist in my scene!
It comes up with the error
NullReferenceException: Object reference not set to an instance of an object
BulletScript.Awake () (at Assets/BulletScript.js:23)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
When I double click on the error it sends me here. Highlighting “if(gun.GetComponent(Gun).aiming == true)”
Bullet Script:
#pragma strict
var bulletHole : GameObject;
var floatInFrontOfWall : float = 0.001;
var maxDist : float = 10000000000;
var Enemy : GameObject;
var maxDamage : float = 15;
var minDamage : float = 5;
var gun : GameObject;
var blood : GameObject;
//Accuracy
var minAimAccuracy : float;
var maxAimAccuracy : float;
var minHipAccuracy : float;
var maxHipAccuracy : float;
function Awake () {
gun = GameObject.FindWithTag("Gun");
if(gun.GetComponent(Gun).aiming == true)
{
transform.rotation.eulerAngles.y += Random.Range(minAimAccuracy, maxAimAccuracy);
transform.rotation.eulerAngles.x += Random.Range(minAimAccuracy, maxAimAccuracy);
}
if(gun.GetComponent(Gun).aiming == false)
{
transform.rotation.eulerAngles.y += Random.Range(minHipAccuracy, maxHipAccuracy);
transform.rotation.eulerAngles.x += Random.Range(minHipAccuracy, maxHipAccuracy);
}
}
function Update () {
var hit : RaycastHit;
var damage = Random.Range(minDamage, maxDamage);
if(Physics.Raycast(transform.position, transform.forward,hit, maxDist))
{
if(bulletHole && hit.transform.tag == "Terrain")
Instantiate(bulletHole, hit.point +(hit.normal * floatInFrontOfWall),Quaternion.LookRotation(hit.normal));
if(hit.transform.tag == "Enemy")
{
Enemy = hit.collider.gameObject;
}
if(Enemy)
Enemy.GetComponent(EnemyHealth).currentHealth -= damage;
}
Destroy(gameObject);
}
I have other objects in my scene with the tag “Gun” but it doesn’t seem to want to use those
Please help!