0

I have a Problem in Unity where where When when i use specific weapon in game i created and try to instantiate the lauching of a projectile i receive the following error:

Object reference not set to an instance of an object

I dont undestand the problem in my code as i have instantiated the prefab. I am currently following a tutorial in a booki have also searched on the internet and cant find someone with the same problem any help is appreciated:

if (currentWeapon == WEAPON_GRENADE && ammos [WEAPON_GRENADE] >=1 && canShoot)
        {
            ammos [currentWeapon]--;
            GameObject launcher = GameObject.Find("launcher");
            GameObject grenadeF = (GameObject) (Instantiate (grenade, launcher.transform.position, Quaternion.identity));
            grenadeF.GetComponent<Rigidbody>().AddForce(launcher.transform.forward*200);
            canShoot = false;
            timer = 0.0f;
            timerStarted = true;
        }

This is the piece of code;

i have tried to change:

grenadeF

to

grenade

.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ManageWeapons : MonoBehaviour {




private const int WEAPON_GUN = 0;
private const int WEAPON_AUTO_GUN = 1;
private const int WEAPON_GRENADE = 2;

private int activeWeapon = WEAPON_GUN;
private float timer;
private bool timerStarted;
private bool canShoot = true;
private int currentWeapon;

private bool [] hasWeapon;
private int [] ammos;
private int [] maxAmmos;
private float [] reloadTime;
private string [] weaponName;   

// Use this for initialization
Camera playersCamera;
Ray rayFromPlayer;
RaycastHit hit;
public GameObject sparksAtImpact;
private int gunAmmo = 10;

public GameObject grenade;  
// Use this for initialization
void Start () 
{
    ammos = new int [3];
    hasWeapon = new bool [3];
    maxAmmos = new int [3];
    reloadTime = new float [3]; 
    weaponName = new string [3];

    hasWeapon [WEAPON_GUN] = true;
    hasWeapon [WEAPON_AUTO_GUN] = true;
    hasWeapon [WEAPON_GRENADE] = true;

    weaponName[WEAPON_GUN] = "GUN";     
    weaponName[WEAPON_AUTO_GUN] = "AUTOMATIC GUN";      
    weaponName[WEAPON_GRENADE] = "GRENADE";     

    ammos [WEAPON_GUN] = 10;
    ammos [WEAPON_AUTO_GUN] = 10;
    ammos [WEAPON_GRENADE] = 10;


    maxAmmos [WEAPON_GUN] = 20;
    maxAmmos [WEAPON_AUTO_GUN] = 20;
    maxAmmos [WEAPON_GRENADE] = 10;

    currentWeapon = WEAPON_GUN;

    playersCamera = GetComponent<Camera>(); 
    reloadTime [WEAPON_GUN] = 2.0f;
    reloadTime [WEAPON_AUTO_GUN] = 0.5f;
    reloadTime [WEAPON_GRENADE] = 3.0f;

}


// Update is called once per frame
void Update () 
{
    if (timerStarted)
    {
        timer += Time.deltaTime;
        if (timer >= reloadTime [currentWeapon])
        {
            timerStarted = false;
            canShoot = true;
        }
    }

    rayFromPlayer = playersCamera.ScreenPointToRay (new Vector3 (Screen.width/2, Screen.height/2, 0));
    Debug.DrawRay(rayFromPlayer.origin, rayFromPlayer.direction * 100, Color.red);
    //if (Input.GetKeyDown(KeyCode.F) && gunAmmo>0)
    //if (Input.GetKeyDown(KeyCode.F))
    if (Input.GetKey(KeyCode.F))
    {
        //if (currentWeapon == WEAPON_GUN && ammos [WEAPON_GUN] >=1 && canShoot)
        if ((currentWeapon == WEAPON_GUN  || currentWeapon == WEAPON_AUTO_GUN) && ammos [currentWeapon] >=1 && canShoot)
        {
            ammos [currentWeapon]--;
            GetComponent<AudioSource>().Play();
            if (Physics.Raycast(rayFromPlayer, out hit, 100))
            {
                print (" The object " + hit.collider.gameObject.name +" is in front of the player");
                Vector3 positionOfImpact;
                positionOfImpact = hit.point;
                Instantiate (sparksAtImpact, positionOfImpact, Quaternion.identity);
                GameObject objectTargeted;
                if (hit.collider.gameObject.tag == "target")
                {
                    objectTargeted = hit.collider.gameObject;
                    objectTargeted.GetComponent<ManageNPC>().gotHit();
                }

            }
            canShoot = false; 
            timer = 0.0f;
            timerStarted = true;

            //gunAmmo --;
            //print ("You have "+gunAmmo + " bullets left");
        }
        if (currentWeapon == WEAPON_GRENADE && ammos [WEAPON_GRENADE] >=1 && canShoot)
        {
            ammos [currentWeapon]--;
            GameObject launcher = GameObject.Find("launcher");
            GameObject grenadeF = (GameObject) (Instantiate (grenade, launcher.transform.position, Quaternion.identity));
            grenadeF.GetComponent<Rigidbody>().AddForce(launcher.transform.forward*200);
            canShoot = false;
            timer = 0.0f;
            timerStarted = true;
        }


    }

    if (Input.GetKeyDown(KeyCode.Tab))
    {
        if (hasWeapon[WEAPON_GUN] && hasWeapon[WEAPON_AUTO_GUN] && hasWeapon[WEAPON_GRENADE]) 
        {
            currentWeapon++;
            if (currentWeapon>2) currentWeapon = 0;
        }
        else if (hasWeapon[WEAPON_GUN] && hasWeapon[WEAPON_AUTO_GUN])
        {
            if (currentWeapon == WEAPON_GUN) currentWeapon = WEAPON_AUTO_GUN;
            else currentWeapon = WEAPON_GUN;            
        }
        else if (hasWeapon[WEAPON_GUN] && hasWeapon[WEAPON_GRENADE])
        {
            if (currentWeapon == WEAPON_GUN) currentWeapon = WEAPON_GRENADE;
            else currentWeapon = WEAPON_GUN;                
            }
        else if (hasWeapon[WEAPON_AUTO_GUN] && hasWeapon[WEAPON_GRENADE])
        {
            if (currentWeapon == WEAPON_AUTO_GUN) currentWeapon = WEAPON_GRENADE;
            else currentWeapon = WEAPON_AUTO_GUN;       
        }
        else
        {
        }
        print ("Current Weapon: "+ weaponName[currentWeapon] + "("+ammos[currentWeapon]+")");
    }


    GameObject.Find("userInfo").GetComponent<Text>().text = weaponName[currentWeapon]+ "("+ammos[currentWeapon]+")";

}

public void manageCollisions (ControllerColliderHit hit)
{
    /*print ("Collided with " + hit.collider.gameObject.name);
    if (hit.collider.gameObject.tag =="ammo_gun")
    {
        gunAmmo +=5;
        if (gunAmmo > 10) gunAmmo = 10;
        Destroy (hit.collider.gameObject);
    }*/
    string tagOfTheOtherObject = hit.collider.gameObject.tag;
    if (tagOfTheOtherObject == "ammo_gun" || tagOfTheOtherObject == "ammo_automatic_gun" || tagOfTheOtherObject == "ammo_grenade")
    {
        int indexOfAmmoBeingUpdated = 0;
        if (tagOfTheOtherObject =="ammo_gun") indexOfAmmoBeingUpdated = WEAPON_GUN;
        if (tagOfTheOtherObject =="ammo_automatic_gun") indexOfAmmoBeingUpdated = WEAPON_AUTO_GUN;
        if (tagOfTheOtherObject =="ammo_grenade") indexOfAmmoBeingUpdated = WEAPON_GRENADE;
        ammos [indexOfAmmoBeingUpdated] +=5;
        if (ammos [indexOfAmmoBeingUpdated] > maxAmmos[indexOfAmmoBeingUpdated]) ammos[indexOfAmmoBeingUpdated] = maxAmmos[indexOfAmmoBeingUpdated];
        Destroy (hit.collider.gameObject);
    }           




}
}

This is the full script if anyone is interest

And here are images of unity:

enter image description here

enter image description here

As you can see the prefabs are here.

Daniel
  • 11
  • 3
  • To elaborate on one point: You have a public GameObject grenade field where you reference your prefab "grenade". Therefore, you can't rename a local variable to "granade" as well, while "grenadeF" would be fine. In which line is the error thrown or what is the exact message? – Juri Knauth Jan 15 '22 at 20:47
  • @JuriKnauth The error message is given on line 119 or the line :GameObject grenadeF = (GameObject) (Instantiate (grenade, launcher.transform.position, Quaternion.identity)); – Daniel Jan 16 '22 at 11:47
  • In the above line you are looking for a GameObject by name called "launcher", but in your hierarchy I see that it is called "laucher". Simply a typo. So as you see it's probably better to reference your launcher transform directly via a public or serializable field. Or assign it a script and use FindObjectOfType(); and create an empty Launcher.cs script that inherits from MonoBehaviour and assign it to the laucher GameObject. – Juri Knauth Jan 16 '22 at 13:02
  • @JuriKnauth Thank you very much. Really stupid mistake from my part i thought what was wrong was in the script but anyways it works now so thanks again. – Daniel Jan 16 '22 at 17:02

0 Answers0