Question by
Gohst002 · Sep 19, 2016 at 10:12 AM ·
shootinggun script
NullReferenceExeption on my gun
Hi guys. I found this tutorial and updated the script but my gun does not fire.
using UnityEngine; using System.Collections;
public class Shoot : MonoBehaviour
{
public GameObject ammoPrefab;
private Transform myTransform;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0))
Debug.Log("Pressed Left Click.");
{
spawnAmmo ();
}
}
void SetInitialReferences ()
{
myTransform = transform;
}
void spawnAmmo()
{
Instantiate(ammoPrefab,myTransform.TransformPoint(0,0,0.5f),myTransform.rotation);
}
}
It spams my console with:
NullReferenceExeption: Object reference not set to an instance of an object Shoot.spawnAmmo () (at Assets/Scripts/Shoot.cs:31)
I am a beginner so I do not know what to do. Any help would be appreciated :D
Comment
Answer by anil4029 · Sep 19, 2016 at 10:41 AM
Change your update method to this :
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
Debug.Log("Pressed Left Click.");
spawnAmmo ();
}
}
have a look at your rotation and change your if statement