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

NullReferenceException:

Discussion in 'Scripting' started by Kiesco91, Jul 3, 2021.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hello, im getting a NullReferenceException but as far as im concerned, im calling everything I need to call?

    Shoot Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon;
    6.  
    7. public class Shooting : MonoBehaviourPun
    8. {
    9.     private int weaponDamage;
    10.     private string weaponName;
    11.     private GameObject ShootPoint;
    12.  
    13.  
    14.     public Shooting(string weaponName, int weaponDamage)
    15.     {
    16.         this.weaponName = weaponName;
    17.         this.weaponDamage = weaponDamage;
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         foreach (Transform t in gameObject.transform)
    23.         {
    24.             if (t.tag == "Bullet")
    25.             {
    26.                 ShootPoint = t.gameObject;
    27.             }
    28.         }
    29.     }
    30.  
    31.     public void Shoot()
    32.     {
    33.         photonView.RPC("RpcShoot", RpcTarget.All);
    34.     }
    35.  
    36.     [PunRPC]
    37.     public void RpcShoot()
    38.     {
    39.         GameObject bullet = Instantiate((GameObject)Resources.Load("Bullet"), ShootPoint.transform.position, ShootPoint.transform.rotation);
    40.         Rigidbody br = GetComponent<Rigidbody>();
    41.         br.AddRelativeForce(Vector3.forward * 1000 * Time.deltaTime, ForceMode.Impulse);
    42.     }
    43.  
    44. }
    45.  
    and this is the Shoot part of my PlayerController Script:
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         if (!PV.IsMine)
    4.             return;
    5.  
    6.         RB.MovePosition(RB.position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
    7.  
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             GetComponent<Shooting>().Shoot();
    11.         }
    12.  
    13.     }
    Why am I getting a NullRefException when to me, im calling a GameObject from resources?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Unfortunately, it doesn't really matter what you think.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hi Kurt,
    Thanks for your response, ive managed to get that issue sorted but now I have another issue (as is the way with coding).

    ive got my bullet shooting out straight absolutely fine. I can't shoot up or down though. I can rotate 360 horizontally fine and it will shoot in whatever direction im looking but when I look up or down, it doesn't shoot up or down, it still fires straight out from shoot point. I thought this would be to do with my shoot point vertical rotation not changing but I have it attached to my camera so when my camera looks up and down, that should rotate with it but it doesn't.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon;
    6.  
    7. public class Shooting : MonoBehaviourPun
    8. {
    9.     private int weaponDamage;
    10.     private string weaponName;
    11.     public GameObject ShootPoint;
    12.     public GameObject _bullet;
    13.     public float bulletForce;
    14.  
    15.  
    16.     public Shooting(string weaponName, int weaponDamage)
    17.     {
    18.         this.weaponName = weaponName;
    19.         this.weaponDamage = weaponDamage;
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.  
    25.     }
    26.  
    27.     public void Shoot()
    28.     {
    29.         photonView.RPC("RpcShoot", RpcTarget.All);
    30.     }
    31.  
    32.     [PunRPC]
    33.     public void RpcShoot()
    34.     {
    35.         GameObject TempBulletHandler;
    36.         TempBulletHandler = Instantiate(_bullet, ShootPoint.transform.position, ShootPoint.transform.rotation) as GameObject;
    37.  
    38.         Rigidbody tempRB;
    39.         tempRB = TempBulletHandler.GetComponent<Rigidbody>();
    40.  
    41.         tempRB.AddForce(gameObject.transform.forward * bulletForce);
    42.  
    43.         Destroy(TempBulletHandler, 10.0f);
    44.     }
    45.  
    46. }
    ive uploaded a screenshot of my Hierarchy so you can see how it breaks down.

    I can't work out why it doesn't shoot up or down :(
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This line means "use the transform of where this script is located for rotation (eg forward)"

    Do you mean instead to use the direction of TempBulletHandler, since you gave it an initial rotation?

    Or you can use ShootPoint.transform.forward to be consistent...
     
  5. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    I don't know why im struggling with this so much right now. I can fire and shoot in the direction but it isn't syncing across the network. Player 1 can fire and see their bullets and player 2 can fire and see their bullets but they can't see each others and its throwing up a Null Ref exception again saying "GameObject" is being destroyed but ive commented out the destroy line so I don't know why its saying this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon;
    6.  
    7. public class Shooting : MonoBehaviourPun
    8. {
    9.     private int weaponDamage;
    10.     private string weaponName;
    11.     public GameObject ShootPoint;
    12.     public GameObject _bullet;
    13.     public float bulletForce;
    14.  
    15.  
    16.     public Shooting(string weaponName, int weaponDamage)
    17.     {
    18.         this.weaponName = weaponName;
    19.         this.weaponDamage = weaponDamage;
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.  
    25.     }
    26.  
    27.     public void Shoot()
    28.     {
    29.         photonView.RPC("RpcShoot", RpcTarget.All);
    30.     }
    31.  
    32.     [PunRPC]
    33.     public void RpcShoot()
    34.     {
    35.         GameObject TempBulletHandler;
    36.         TempBulletHandler = PhotonNetwork.Instantiate(_bullet.name, ShootPoint.transform.position, ShootPoint.transform.rotation) as GameObject;
    37.  
    38.         Rigidbody tempRB;
    39.         tempRB = TempBulletHandler.GetComponent<Rigidbody>();
    40.  
    41.         tempRB.AddForce(ShootPoint.transform.forward * bulletForce);
    42.  
    43.         //Destroy(TempBulletHandler, 10.0f);
    44.     }
    45.  
    46. }
    47.  
    My bullet has a Photon View attached as I want visual of the bullet but I don't plan on having lots and lots on the screen at one time so don't think this will be a performance issue.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    NullReferenceExceptions have a line number tied to them, try reading the error carefully to pinpoint where the problem is occurring in your code.
     
  7. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hello,

    thanks for your response. ive only just got chance to go back and look at this error and its saying its line 36.

    ive honestly no clue why its erroring.

    MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.

    Line 36 is me instantiating it and it instantiates on the player doing the shooting fine. the error comes from the other players game.

    I read somewhere that an RPC can't be a public void so tried that but still no luck :(

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon;
    6.  
    7. public class Shooting : MonoBehaviourPun
    8. {
    9.     private int weaponDamage;
    10.     private string weaponName;
    11.     public GameObject ShootPoint;
    12.     public GameObject _bullet;
    13.     public float bulletForce;
    14.  
    15.  
    16.     public Shooting(string weaponName, int weaponDamage)
    17.     {
    18.         this.weaponName = weaponName;
    19.         this.weaponDamage = weaponDamage;
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.  
    25.     }
    26.  
    27.     public void Shoot()
    28.     {
    29.         photonView.RPC("RpcShoot", RpcTarget.All);
    30.     }
    31.  
    32.     [PunRPC]
    33.     void RpcShoot()
    34.     {
    35.         GameObject TempBulletHandler;
    36.         TempBulletHandler = PhotonNetwork.Instantiate(_bullet.name, ShootPoint.transform.position, ShootPoint.transform.rotation);
    37.  
    38.         Rigidbody tempRB;
    39.         tempRB = TempBulletHandler.GetComponent<Rigidbody>();
    40.  
    41.         tempRB.AddForce(ShootPoint.transform.forward * bulletForce);
    42.  
    43.         //Destroy(TempBulletHandler, 10.0f);
    44.     }
    45.  
    46. }