- Home /
Unity c# Object reference not set to an instance of an Object ( Shooting Script )
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public string Name;
public int RateOfFire;
int ROF;
public int Accuracy;
public int Ammo;
public Bullet Amunition;
public PCP shootingPoint;
[HideInInspector]
public bool IsActive = false;
void Start ()
{
ROF = 0;
}
// Update is called once per frame
public void WeaponUpdate ()
{
if(ROF != 0)
{
ROF --;
}
}
public void Shoot()
{
if(Ammo > 0 && ROF == 0)
{
shootingPoint.SendMessage("Create",Amunition);
Ammo --;
ROF = RateOfFire;
}
}
"Note : PCP is shortcut to Prefab Shooting Point".
I got this error in the shooting method in the line "shootingPoint.SendMessage" I just dont understand why ? and I have the Components at the objects I placed in the shootingPoint and the Ammunation , so what is wrong ??
Here is an image to prove I attached objects :
PCP code : using UnityEngine; using System.Collections;
public class PCP : MonoBehaviour {
/*
* Prefab Creation Point
*/
void Update ()
{
}
public void Create(Bullet bullet)
{
Bullet clone;
clone = (Bullet) Instantiate (bullet, transform.position, transform.rotation);
clone.rigidbody2D.velocity = transform.TransformDirection (Vector2.right * bullet.Speed);
}
}
Seems like the exception is thrown from the PCP.Create method, if we could see this code...
Why do you need to call send message? You have the reference, just call the function.
I assume that bullet.speed is initialised?
Use a debug.log to check if shootingPoint has a value. You are not accidently clearing it somewhere else?
Looks like the reference to your bullet is empty...