Question by
El Maxo · Jan 08, 2016 at 11:36 AM ·
nullreferenceexception
Object reference not set to an instance of an object C#
Hi guys,
I am currently trying to code a script that will allow me to spawn a prefab and add force, I have got half way, allowing it to spawn in. my issue arises when I try add force to the spawned prefab. Have had an issue complying my prototype code, but when I run it I have an error appear :
Object reference not set to an instance of an object Gun1ShootScript.Update () (at Assets/Scripts/Gun1ShootScript.cs:20)
From what I have looked into It looks like it is struggling to find the prefab, My question is how should I apply force to this?
using UnityEngine;
using System.Collections;
public class Gun1ShootScript : MonoBehaviour {
public GameObject Bullet1;
public float speed = 20;
Rigidbody RB;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(Bullet1, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0f, 0f));
}
}
}
Comment
Yes I do, tried editing some of the properties on it but to no avail