Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Ansar_05, Jul 2, 2022.

  1. Ansar_05

    Ansar_05

    Joined:
    Apr 5, 2022
    Posts:
    3
    Hi,
    how to fix this problem (Exception NullReferenceException: the reference to the object is not set for the instance of the object
    Evolved games.Exchange of items.Fixed update () (in Assets/Evolvedgames/Realistic FPS Controller/Scripts/itemChange.cs:104)
    The whole code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    namespace EvolveGames
    {
    public class ItemChange : MonoBehaviour
    {
    [Header("Item Change")]
    [SerializeField] public Animator ani;
    [SerializeField] Image ItemCanvasLogo;
    [SerializeField] bool LoopItems = true;
    [SerializeField, Tooltip("You can add your new item here.")] GameObject[] Items;
    [SerializeField, Tooltip("These logos must have the same order as the items.")] Sprite[] ItemLogos;
    [SerializeField] int ItemIdInt;
    int MaxItems;
    int ChangeItemInt;
    [HideInInspector] public bool DefiniteHide;
    bool ItemChangeLogo;

    private void Start()
    {
    if (ani == null && GetComponent<Animator>()) ani = GetComponent<Animator>();
    Color OpacityColor = ItemCanvasLogo.color;
    OpacityColor.a = 0;
    ItemCanvasLogo.color = OpacityColor;
    ItemChangeLogo = false;
    DefiniteHide = false;
    ChangeItemInt = ItemIdInt;
    ItemCanvasLogo.sprite = ItemLogos[ItemIdInt];
    MaxItems = Items.Length - 1;
    StartCoroutine(ItemChangeObject());
    }
    private void Update()
    {
    if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    {
    ItemIdInt++;
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    {
    ItemIdInt--;
    }

    if(Input.GetKeyDown(KeyCode.H))
    {
    if (ani.GetBool("Hide")) Hide(false);
    else Hide(true);
    }

    if (ItemIdInt < 0) ItemIdInt = LoopItems ? MaxItems : 0;
    if (ItemIdInt > MaxItems) ItemIdInt = LoopItems ? 0 : MaxItems;


    if (ItemIdInt != ChangeItemInt)
    {
    ChangeItemInt = ItemIdInt;
    StartCoroutine(ItemChangeObject());
    }
    }

    public void Hide(bool Hide)
    {
    DefiniteHide = Hide;
    ani.SetBool("Hide", Hide);
    }

    IEnumerator ItemChangeObject()
    {
    if(!DefiniteHide) ani.SetBool("Hide", true);
    yield return new WaitForSeconds(0.3f);
    for (int i = 0; i < (MaxItems + 1); i++)
    {
    Items.SetActive(false);
    }
    Items[ItemIdInt].SetActive(true);
    if (!ItemChangeLogo) StartCoroutine(ItemLogoChange());

    if (!DefiniteHide) ani.SetBool("Hide", false);
    }

    IEnumerator ItemLogoChange()
    {
    ItemChangeLogo = true;
    yield return new WaitForSeconds(0.5f);
    ItemCanvasLogo.sprite = ItemLogos[ItemIdInt];
    yield return new WaitForSeconds(0.1f);
    ItemChangeLogo = false;
    }

    private void FixedUpdate()
    {

    if (ItemChangeLogo)
    {
    Color OpacityColor = ItemCanvasLogo.color;
    OpacityColor.a = Mathf.Lerp(OpacityColor.a, 0, 20 * Time.deltaTime);
    ItemCanvasLogo.color = OpacityColor;
    }
    else
    {
    Color OpacityColor = ItemCanvasLogo.color;
    OpacityColor.a = Mathf.Lerp(OpacityColor.a, 1, 6 * Time.deltaTime);
    ItemCanvasLogo.color = OpacityColor;
    }
    }
    }

    }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
  3. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    443
    According to the given description, the issue is that "ItemCanvasLogo" is null. You probably haven't assigned the variable in the inspector (unlikely given at which line the error was thrown - however it still could be the case if you posted the last time the error was thrown instead of the first time, but this is speculation) or the ItemCanvasLogo Image has been destroyed at a given point in time and therefore is null.