Hi guys, this is my first post so apologies if I post anything not the way it’s supposed to. I’m just starting to use Unity so I’m not able to debug everything on my own. I’m trying to instantiate a bullet when I left mouse click, and when it’s instantiated a force is applied to it. This issue I’m having is as it says in the title, is that an Object reference is not set to an instance of an object. Here’s my code:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour
{
//bool shoot = false;
public Transform prefabBullet;
public float shootForce;
GameObject instanceBullet;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity) as GameObject;
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
}
In the scene the bullet is initiated, it just doesn’t have any force applied to it. They just keep spawning on my player and stack up. If anyone could help that would be much appreciated.