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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I keep getting null reference

Discussion in 'Scripting' started by RhinoRunnerGaming29, Jun 8, 2022.

  1. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    Here is my error:

    NullReferenceException: Object reference not set to an instance of an object


    Here is my code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace RhinoRunnerGaming
    7. {
    8.     public class weapons : MonoBehaviour
    9.     {
    10.         #region Variables
    11.  
    12.         public gun[] loadout;
    13.         public Transform weaponParent;
    14.         public GameObject bulletHolePrefab;
    15.         public LayerMask canBeShot;
    16.  
    17.         private int currentIndex;
    18.         private GameObject currentWeapon;
    19.  
    20.         #endregion
    21.  
    22.         #region Monobehavior Callbacks
    23.  
    24.         void Update()
    25.         {
    26.             if (Input.GetKeyDown(KeyCode.Alpha1))
    27.             {
    28.                 Equip(0);
    29.             }
    30.  
    31.             if (currentWeapon != null)
    32.             {
    33.                 Aim(Input.GetMouseButton(1));
    34.  
    35.                 if (Input.GetMouseButtonDown(0))
    36.                 {
    37.                     Shoot();
    38.                 }
    39.             }
    40.  
    41.         }
    42.  
    43.         #endregion
    44.  
    45.         #region Private Methods
    46.  
    47.         void Equip(int p_ind)
    48.         {
    49.             if (currentWeapon != null) Destroy(currentWeapon);
    50.  
    51.             currentIndex = p_ind;
    52.  
    53.             GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
    54.             t_newWeapon.transform.localPosition = Vector3.zero;
    55.             t_newWeapon.transform.localEulerAngles = Vector3.zero;
    56.  
    57.             currentWeapon = t_newWeapon;
    58.         }
    59.         void Aim(bool p_isAiming)
    60.         {
    61.             Transform t_anchor = currentWeapon.transform.Find("anchor");
    62.             Transform t_state_ads = currentWeapon.transform.Find("states/ADS");
    63.             Transform t_state_hip = currentWeapon.transform.Find("states/hips");
    64.  
    65.             if (p_isAiming)
    66.             {
    67.                 //aim
    68.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimspeed);
    69.             }
    70.             else
    71.             {
    72.                 //hip
    73.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimspeed);
    74.             }
    75.         }
    76.  
    77.         void Shoot()
    78.         {
    79.             Transform t_spawn = transform.Find("Cameras/playerCamera");
    80.  
    81.             RaycastHit t_hit = new RaycastHit();
    82.             if (Physics.Raycast(t_spawn.position, t_spawn.forward, out t_hit, 1000f, canBeShot))
    83.             {
    84.                 GameObject t_newHole = Instantiate(bulletHolePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
    85.                 t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
    86.                 Destroy(t_newHole, 5f);
    87.             }
    88.         }
    89.  
    90.         #endregion
    91.     }
    92.  
    93. }

    can somebody please help me:)
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    On what line the error appears ?
     
  3. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    lines 82 and 37
     
  4. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    lines 82 and 37
     
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    Looks like
    Code (csharp):
    1.  
    2. Transform t_spawn = transform.Find("Cameras/playerCamera");
    3.  
    Is not finding any object with the name "Cameras/playerCamera"

    Try "playerCamera" since i guess this is the name of a GameObject

    Or better don't find objects by name.

    And the most important, learn how to use debugger.
     
  6. Joshd1410

    Joshd1410

    Joined:
    May 7, 2021
    Posts:
    35
    Can you try changing line 81 to the following, removing the "new" declaration:

    RaycastHit t_hit;

    May not be the issue but worth a try. I've never declared a raycasthit as new before and never had any issues with getting them to work.

    Only other thing I could think it might be - are you sure the reference to t_spawn is getting set correctly beforehand?
     
  7. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    thank you so much, that was the problem!

    my camera is not Cameras it's just camera/playerCamera
    simple mistake