Search Unity

NullReferenceException: Object reference not set to an instance of an object GameManager.Awake () (a

Discussion in 'Scripting' started by unity_zmQqOLfYTWv1oQ, Feb 19, 2021.

?

How solve NullReferenceException

  1. dont know

    2 vote(s)
    50.0%
  2. dont know

    2 vote(s)
    50.0%
  1. unity_zmQqOLfYTWv1oQ

    unity_zmQqOLfYTWv1oQ

    Joined:
    Feb 12, 2020
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using DG.Tweening;
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     private GameObject karePrefab;
    11.  
    12.     [SerializeField]
    13.     private Text soruText;
    14.  
    15.     [SerializeField]
    16.     private Transform karelerPaneli;
    17.  
    18.  
    19.     private GameObject[] karelerDizisi = new GameObject[25];
    20.  
    21.     [SerializeField]
    22.     private Transform soruPaneli;
    23.  
    24.     List<int> bolumDegerleriListesi = new List<int>();
    25.  
    26.     int bolunenSayi, bolenSayi;
    27.     int kacinciSoru;
    28.     int dogruSonuc;
    29.  
    30.     int butonDegeri;
    31.  
    32.     bool butonaBasilsinmi;
    33.  
    34.     int kalanHak;
    35.  
    36.     KalanHaklarManager kalanHaklarManager;
    37.  
    38.  
    39.     private void Awake()
    40.     {
    41.         kalanHak = 3;
    42.        
    43.         kalanHaklarManager = Object.FindObjectOfType<KalanHaklarManager>();
    44.        
    45.         kalanHaklarManager.KalanHaklariKontrolEt(kalanHak);
    46.     }
    47.      
    48.  
    49.  
    50.  
    51.  
    52.     void Start()
    53.     {
    54.         butonaBasilsinmi = false;
    55.        
    56.         soruPaneli.GetComponent<RectTransform>().localScale = Vector3.zero;
    57.        
    58.         kareleriOlustur();
    59.     }
    60.    
    61.  
    62.    public void kareleriOlustur()
    63.     {
    64.         for (int i = 0; i < karelerDizisi.Length; i++)
    65.  
    66.         {
    67.             GameObject kare = Instantiate(karePrefab, karelerPaneli);
    68.             kare.transform.GetComponent<Button>().onClick.AddListener(() => ButonaBasildi());
    69.             karelerDizisi[i] = kare;
    70.         }
    71.         BolumDegerleriniTexteYazdır();
    72.         StartCoroutine(DoFadeRoutine());
    73.         Invoke("SoruPaneliniAc" , 2f);
    74.  
    75.  
    76.     }
    77.  
    78.     void ButonaBasildi()
    79.         {
    80.         if (butonaBasilsinmi == true)
    81.         {
    82.             butonDegeri = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.transform.GetChild(0).GetComponent<Text>().text);
    83.  
    84.             SonucuKontrolEt();
    85.         }
    86.     void SonucuKontrolEt()
    87.         {
    88.             if(butonDegeri==dogruSonuc)
    89.             {
    90.                 Debug.Log("dogru sonuc");
    91.  
    92.             }else
    93.             {
    94.                 kalanHak--;
    95.                 kalanHaklarManager.KalanHaklariKontrolEt(kalanHak);
    96.             }
    97.         }
    98.        
    99.        
    100.     }
    101.     IEnumerator DoFadeRoutine()
    102.     {
    103.         foreach(var kare in karelerDizisi)
    104.         {
    105.             kare.GetComponent<CanvasGroup>().DOFade(1, 0.2F);
    106.  
    107.             yield return new WaitForSeconds(0.07f);
    108.         }
    109.     }
    110.  
    111.     void BolumDegerleriniTexteYazdır()
    112.     {
    113.         foreach(var kare in karelerDizisi)
    114.         {
    115.             int rastgeleDeger = Random.Range(1, 13);
    116.             bolumDegerleriListesi.Add(rastgeleDeger);
    117.  
    118.             kare.transform.GetChild(0).GetComponent<Text>().text = rastgeleDeger.ToString();
    119.  
    120.         }
    121.  
    122.     }
    123.  
    124.     void SoruPaneliniAc()
    125.     {
    126.         SoruyuSor();
    127.         butonaBasilsinmi = true;
    128.        
    129.         soruPaneli.GetComponent<RectTransform>().DOScale(1, 0.3f).SetEase(Ease.OutBack);
    130.  
    131.  
    132.     }
    133.  
    134.     void SoruyuSor()
    135.     {
    136.         bolenSayi = Random.Range(2, 11);
    137.  
    138.         kacinciSoru = Random.Range(0, bolumDegerleriListesi.Count);
    139.  
    140.         dogruSonuc = bolumDegerleriListesi[kacinciSoru];
    141.        
    142.         bolunenSayi = bolenSayi * dogruSonuc;
    143.  
    144.         soruText.text = bolunenSayi.ToString() + " : " + bolenSayi.ToString();
    145.     }
    146.  
    147.  
    148.  
    149.  
    150.  
    151.  
    152. }
    153.  
    154.  
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Have a look at the line number in the error. Something on that line number is null and you're tyring to reference it.

    Something in Awake() by the looks of the error.

    Likely candidate is kalanHaklarManager on 45.
     
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    356
    You probably need to add KalanHaklarManager in hierachy window.
    Code (CSharp):
    1. kalanHaklarManager = Object.FindObjectOfType<KalanHaklarManager>();
    2. //Your are trying to find object, but engine can't find it. Field is null.
    3.  
    4. kalanHaklarManager.KalanHaklariKontrolEt(kalanHak);
    5. //Calling method on null => nullReferenceException
     
    mopthrow likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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.