Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Picking a Transform from a GameObject, but got Object reference not set to an instance of an object

Discussion in 'Scripting' started by Crax, Oct 21, 2014.

  1. Crax

    Crax

    Joined:
    Apr 11, 2013
    Posts:
    13
    So, this i'm writing a weapon handling system, and this is the logic i want to use
    First: in a Weapon List i add some weapons classes, and at the beginning of the game i set the weapon.
    By doing so, i get the muzzle position of the weapon, and i guess that the problem is there
    With some Debug.Log(); i checked that Unity finds the bulletSpawn, but i don't know how to solve the problem...

    These are the two classes involved in the problem:


    Code (CSharp):
    1.  
    2. //weapon.cs
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class Weapons : MonoBehaviour
    9. {
    10.     #region private
    11.     private GameObject player; //REMINDER: MATT MUST FIX THE PLAYER IN THE GameManager.cs
    12.     private Transform bulletSpawn;
    13.     private IWeapon ActualWeapon;
    14.     List<IWeapon> weapons;
    15.     #endregion
    16.     #region public
    17.     public Transform weaponPositionOnPlayer;
    18.     public Rigidbody bullet;
    19.     public GameObject[] guns;
    20.     public Animation[] reloadAnimations;
    21.     #endregion
    22.     // Use this for initialization
    23.     void Start () {
    24.         player = GameObject.Find("Player");
    25.         #region Checks
    26.      
    27.      
    28.         if (guns.Length == 0)
    29.         {
    30.             Debug.Log("Load some meshs!");
    31.             EditorApplication.isPaused = true;
    32.         }
    33.        /* if (bulletSpawn.Length == 0)
    34.         {
    35.             Debug.Log("Load some bulletSpawns!");
    36.             EditorApplication.isPaused = true;
    37.         }*/
    38.         if (reloadAnimations.Length == 0)
    39.         {
    40.             Debug.Log("Load some Animations!");
    41.             EditorApplication.isPaused = true;
    42.         }
    43.  
    44.         #endregion
    45.         weapons = new List<IWeapon>
    46.         {
    47.             ///Insert here all the weeapons needed.
    48.             new WeaponsBaseClass(guns[0], bulletSpawn, reloadAnimations[0], 10, 2, 0.5f, 1000f, bullet),
    49.             new RocketLauncher(guns[1], bulletSpawn, reloadAnimations[1], 20, 1, 0.3f, 100, bullet)
    50.         };
    51.         SetWeapon(weapons[1], guns[1]);
    52.     }
    53.  
    54.     // Update is called once per frame
    55.     void Update () {
    56.        // if (Input.GetKey(KeyCode.R)) SetWeapon(weapons[1]);
    57.         if (Input.GetMouseButton(0))
    58.         {
    59.             ActualWeapon.Fire();
    60.         }
    61.     }
    62.     void SetWeapon(IWeapon _weapon, GameObject _gun)
    63.     {
    64.         ActualWeapon = _weapon;
    65.         GameObject tempoGun = Instantiate(_gun, weaponPositionOnPlayer.position, _gun.transform.rotation) as GameObject;
    66.         Debug.Log("New bullet spawn transform is: " + tempoGun.transform.GetChild(0).name);
    67.         tempoGun.transform.parent = player.transform.GetChild(0);
    68.     }
    69. }
    and

    Code (CSharp):
    1. //RocketLauncher.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RocketLauncher :  IWeapon {
    6.     private usefulStuff crax = new usefulStuff();
    7.     private bool canShoot = true;
    8.     public float projectileSpeed;
    9.     public float bulletTime;
    10.     private float t = 0f;
    11.     public Rigidbody bullet;
    12.     public int maxBullets, maxBulletsPerCharger;
    13.     private int actualBullets;
    14.     public Animation reloadAnimation;
    15.     public Transform bulletSpawn;
    16.     public GameObject weaponMesh;
    17.  
    18.     public RocketLauncher(GameObject wm, Transform bs, Animation ra, int mb, int mbpc, float bt, float ps, Rigidbody b)
    19.     {
    20.         weaponMesh = wm;
    21.         bulletSpawn = bs;
    22.         reloadAnimation = ra;
    23.         maxBullets = mb;
    24.         maxBulletsPerCharger = mbpc;
    25.         actualBullets = mbpc;
    26.         bulletTime = bt;
    27.         projectileSpeed = ps;
    28.         bullet = b;
    29.     }
    30.     // Use this for initialization
    31.     public void Fire () {
    32.         if (actualBullets > 0)
    33.         {
    34.             //Check if the player can shoot (REALLY LOL CPTN OBVIOUS STRIKEZ AGAIN )
    35.             if (canShoot)
    36.             {
    37.                 //Logic: Remove a bullet, create it then move it. Easy peasy.
    38.                 actualBullets--;
    39.                 Rigidbody nBullet = GameObject.Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody; /// Unity says that the error is here
    40.              
    41.                 Debug.Log("SHOT!");
    42.  
    43.                 //And now the player cant shoot. Cause nobody wants a sniper mitra.
    44.                 //canShoot = false;
    45.             }
    46.             /*else
    47.             {
    48.                 //All this stuff is to make a little amount of time pass between two bullets
    49.                 if (t <= bulletTime)
    50.                 {
    51.                     t += Time.deltaTime;
    52.                 }
    53.                 else
    54.                 {
    55.                     canShoot = true;
    56.                     t = 0;
    57.                 }
    58.             }*/
    59.         }
    60.         else
    61.         {
    62.             Reload();
    63.         }
    64.     }
    65.  
    66.     // Update is called once per frame
    67.     public void Reload () {
    68.         canShoot = false;
    69.         actualBullets = maxBulletsPerCharger;
    70.         //reloadAnimation.Play();
    71.         //crax.Delay(reloadAnimation.clip.length);
    72.         canShoot = true;
    73.     }
    74. }
    75.  
    If someone can help... well, thanks
     
  2. iwHiteRabbiT

    iwHiteRabbiT

    Joined:
    Feb 15, 2011
    Posts:
    47
    As far as I understand, you have a "Object reference not set to an instance of an object" for "bulletSpawn" ?

    As I see it, bulletSpawn is a private attribute, so is null as much as you don't create it or affect it in its own class (private)... So attribute is always null in your code.

    "guns", in opposite, is public, so Unity call the constructor (array here) and "fill" it with the values saved by the inspector, and do it before invoking the Start method.
     
  3. Crax

    Crax

    Joined:
    Apr 11, 2013
    Posts:
    13
    Guess i founded the error
    In the first script i create some weapons, but with a null BulletSpawn