Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
  1  using System.Collections;
  2  using System.Collections.Generic;
  3  using UnityEngine;
  4  using UnityEngine.UI;
  5  using TMPro;
  6  using UnityEngine.Events;
  7  using UnityEngine.EventSystems;
  8  using UnityEngine.SceneManagement;
  9  
 10  public class Ahivments : MonoBehaviour
 11  {
 12      public int total_Coin_Vanya;
 13      public int Coin_Vanya;
 14      [SerializeField] bool isFirst;
 15      
 16  
 17      public string[] arrayTitles;
 18      public Sprite[] arraySprites;
 19      public GameObject button;
 20      public GameObject content;
 21  
 22      private List<GameObject> list = new List<GameObject>();
 23      private VerticalLayoutGroup _group;
 24  
 25  
 26      // Start is called before the first frame update
 27      void Start()
 28      {
 29      Coin_Vanya = PlayerPrefs.GetInt("Coin_Vanya");
 30      total_Coin_Vanya = PlayerPrefs.GetInt("total_Coin_Vanya");
 31      PlayerPrefs.SetInt("isFirst", isFirst ? 1 : 0);
 32  
 33      RectTransform recT = content.GetComponent<RectTransform>();
 34      recT.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
 35      _group = GetComponent<VerticalLayoutGroup>();
 36      setAchievs();
 37  
 38      if(isFirst)
 39      {
 40         StartCoroutine(IdleFarm());
 41      }
 42     
 43      }
 44  
 45      private void RemovedList()
 46      {
 47       foreach (var elem in list)
 48       {
 49        Destroy(elem);
 50       }
 51       list.Clear();
 52      }
 53  
 54      void setAchievs()
 55      {
 56       RectTransform recT = content.GetComponent<RectTransform>();
 57       recT.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
 58       RemovedList();
 59       if (arrayTitles.Length > 0)
 60      {
 61        var pr1 = Instantiate(button, transform);
 62        var h = pr1.GetComponent<RectTransform>().rect.height;
 63        var tr = GetComponent<RectTransform>();
 64        tr.sizeDelta = new Vector2(tr.rect.width, h * arrayTitles.Length);
 65        Destroy(pr1);
 66        for (var i = 0; i < arrayTitles.Length; i++)
 67        {
 68         var pr = Instantiate(button, transform);
 69         pr.GetComponentInChildren<Text>().text = arrayTitles[i];
 70         pr.GetComponentsInChildren<Image>()[1].sprite = arraySprites[i];
 71         var i1 = i;
 72         pr.GetComponent<Button>().onClick.AddListener(() => GetAchievement(i1));
 73         list.Add(pr);
 74        }
 75      }
 76      }
 77  
 78      void GetAchievement(int id)
 79      {
 80        switch(id)
 81        {
 82  
 83         case 0:
 84         Debug.Log(id);
 85         break;
 86  
 87         case 1:
 88         Debug.Log(id);
 89         Coin_Vanya += 10;
 90         PlayerPrefs.SetInt("Coin_Vanya", Coin_Vanya);
 91         break;
 92        }
 93      }
 94  
 95     
 96  
 97      IEnumerator IdleFarm()
 98      {
 99       yield return new WaitForSeconds(1);
100       Coin_Vanya++;
101       Debug.Log(Coin_Vanya);
102       PlayerPrefs.SetInt("Coin_Vanya", Coin_Vanya);
103       StartCoroutine(IdleFarm());
104      }
105  
106      public void ToMeinMenu()
107      {
108      SceneManager.LoadScene(0);
109      }
110  }


What I have tried:

I tried to read the code, but I didn't notice anything, please help

From the comment on solution 1: 69 line and 36
Posted
Updated 18-Dec-22 23:32pm
v2

1 solution

You have not explained which line gives the error, but I assume it is at the end of the IdleFarm method. The method declaration is IEnumerator IdleFarm(), so the compiler expects it to return something at the end. But you have nothing at the end of the method. Try adding yield break; as the last line.
 
Share this answer
 
Comments
Михаил Шпак 19-Dec-22 5:30am    
69 line and 36
Richard MacCutchan 19-Dec-22 5:32am    
Do you not think it would be helpful if you actually showed us which lines they are?
Михаил Шпак 19-Dec-22 5:35am    
pr.GetComponentInChildren<text>().text = arrayTitles[i]; and setAchievs(); the complete error is here "NullReferenceException: Object reference not set to an instance of an object
Ahivments.setAchievs () (at Assets/Scripts/Ahivments.cs:69)
Ahivments.Start () (at Assets/Scripts/Ahivments.cs:36)"
Richard Deeming 19-Dec-22 5:48am    
So why does your question say you're getting a compiler error?

A NullReferenceException simply means you're trying to access a property or call a method on an object which is null. You need to debug your code to find out which object is null when you weren't expecting it to be, then work backwards to find out why it's null.

We can't do that for you.
Richard Deeming 19-Dec-22 5:33am    
Line 69 is:
pr.GetComponentInChildren<Text>().text = arrayTitles[i];

Line 36 is:
setAchievs();

Neither of those lines will produce the error you describe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900