Problem is not reproducible or outdated
Bullet trying to set object that doesn't even exist
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!
Well if I set all the weapons to another tag and change the bullet script to the new tag name it works. But it seems my tag 4 is bugged and no matter what is tagged with "weapon" it will always set it to this mysterious "shotgun(2)"
Answer by Trevdevs · Sep 24, 2016 at 03:00 PM
Unless that first photo was taken during runtime your Gun object is already set to "Shotgun2(Clone)" set that to none and it should hunt the scene for the object with the tag "Gun"
Thanks for the reply! This was taken during runtime. I manged to fix it (sort of as any object using tag 4 will never be noticed. It will always only use Shotgun2(Clone) and I can't just delete the tag and make a new tag as any tag in tag slot 4 will like I said never be noticed. So I just have to waste 1 tag slot :C) I will close down the question now as it seems to be some kind of bug with tagging blender made objects, but I really do appreciate your answer!
Follow this Question
Related Questions
Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer
how to move a gameobject based on a dice roll 1 Answer
Template object and script,GameObject scripts duplicated from a template game object 0 Answers
Infinite floor with object as terrain. 1 Answer
Running C# Function from JS file 0 Answers