@matikYT

Ошибка со скриптом NullReferenceException. Unity что делать?

NullReferenceException: Object reference not set to an instance of an object
Manager.MySave () (at Assets/Scripts/Manager.cs:175)
Manager.GiveScoreAndCoins () (at Assets/Scripts/Manager.cs:138)
SnakeController.OnEndGame () (at Assets/Scripts/SnakeController.cs:110)
UnityEngine.Events.InvokableCall.Invoke () (at :0)
Я использовал PluginYG для яндекс игр и вылазит эта ошибка
Скрипт:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using YG;

public class Manager : MonoBehaviour
{
    public int language;
    public int Score;

    public int Coins;

    public GameObject GOScreen;
    public Text ScoreText;
    public Text CoinText;
    public AudioSource GOAudio;
    public AudioSource Butt;

    public bool Played;
    public bool Menu;

    void Start()
    {
        if (YandexGame.SDKEnabled == true)
        {
            LoadSaveCloud();
            language = YandexGame.savesData.lang;
        }
        language = PlayerPrefs.GetInt("Lang");
    }

    public void OnRusClick()
    {
        language = 0;
        YandexGame.savesData.lang = language;
        PlayerPrefs.SetInt("Lang", language);
        SceneManager.LoadScene("MainMenu");
    }
    public void OnEngClick()
    {
        language = 1;
        YandexGame.savesData.lang = language;
        PlayerPrefs.SetInt("Lang", language);
        SceneManager.LoadScene("MainMenu");
    }

    void Rewarded(int id)
    {
        if (id == 1)
        {
            GameObject SnakeObj;
            SnakeController Snake;
            SnakeObj = GameObject.FindGameObjectWithTag("Player");
            Snake = SnakeObj.GetComponent<SnakeController>();
            Butt.Play();
            Snake.Dead = false;
            GOScreen.SetActive(false);
            Played = false;
            Snake.gameObject.transform.position = new Vector3(-1.0f, 1.18f, -1.0f);
            Snake.gameObject.GetComponent<BoxCollider>().enabled = false;
            Invoke("EnableBC", 2f);
        }
        else if (id == 2)
        {
            Coins += 50;
            MySave();
        }
    }

    public void ExampleOpenRewardAd(int id)
    {
        YandexGame.RewVideoShow(id);
    }

    void Update()
    {
        if(Menu == false)
        {
            GameObject SnakeObj;
            SnakeController Snake;
            SnakeObj = GameObject.FindGameObjectWithTag("Player");
            Snake = SnakeObj.GetComponent<SnakeController>();
            ScoreText.text = Score.ToString();
            CoinText.text = Coins.ToString();
            Menu = false;
            if (Snake.Dead == true)
            {
                GOScreen.SetActive(true);
                if (Played == false)
                {
                    GOAudio.Play();
                    Played = true;
                }
            }
            if (Snake.Dead == false)
            {
                GOScreen.SetActive(false);
                Menu = true;
            }
        }
        else
        {
            if (YandexGame.SDKEnabled == true)
            {
                LoadSaveCloud();
            }
        }
    }

    public void GiveScoreAndCoins()
    {
        GameObject SnakeObj;
        SnakeController Snake;
        SnakeObj = GameObject.FindGameObjectWithTag("Player");
        Snake = SnakeObj.GetComponent<SnakeController>();
        if (Snake.NewScore > Score)
        {
            Score = Snake.NewScore;
        }
        if (Snake.Location == 0)
        {
            Coins += Snake.NewScore * 2;
        }
        if (Snake.Location == 1)
        {
            Coins += Snake.NewScore * 3;
        }
        if (Snake.Location == 2)
        {
            Coins += Snake.NewScore * 4;
        }
        if (Snake.Location == 3)
        {
            Coins += Snake.NewScore * 5;
        }
        MySave(); <---
    }

    private void OnEnable()
    {
        YandexGame.GetDataEvent += LoadSaveCloud;
        YandexGame.RewardVideoEvent += Rewarded;
    }

    private void OnDisable()
    {
        YandexGame.GetDataEvent -= LoadSaveCloud;
        YandexGame.RewardVideoEvent -= Rewarded;
    }

    public void EnableBC()
    {
        GameObject SnakeObj;
        SnakeController Snake;
        SnakeObj = GameObject.FindGameObjectWithTag("Player");
        Snake = SnakeObj.GetComponent<SnakeController>();
        Snake.gameObject.GetComponent<BoxCollider>().enabled = true;
    }

    public void LoadSaveCloud()
    {
        CoinText.text = YandexGame.savesData.coins.ToString();
        ScoreText.text = YandexGame.savesData.score.ToString();
    }

    public void MySave()
    {
        if (Menu == true)
        {
            Skins skins = Camera.main.GetComponent<Skins>();
            YandexGame.savesData.coins = Coins;
            YandexGame.savesData.score = Score;
            YandexGame.savesData.sand = skins.Sand; <---
            YandexGame.savesData.tropic = skins.Tropic;
            YandexGame.savesData.winter = skins.Winter;
            YandexGame.savesData.skin = skins.Skin;
        }
        YandexGame.SaveProgress();
    }
}

И второй скрипт:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class SnakeController : MonoBehaviour
{
    public float MoveSpeed = 5;
    public float SteerSpeed = 100;
    public float BodySpeed = 5;
    public int Gap = 10;
    public int NewScore;
    public int Location;
    public AudioSource Eat;

    public Manager manager;

    public Text NewScoreText;
    public Text NewScoreText2;

    public float steerDirection;

    public FoodSpawner FoodSpawnerController;

    public GameObject BodyPrefab;

    public bool Dead;
    public bool ButtonsUp = true;

    public List<GameObject> BodyParts = new List<GameObject>();
    private List<Vector3> PositionsHistory = new List<Vector3>();

    void Update()
    {
        NewScoreText.text = NewScore.ToString();
        NewScoreText2.text = NewScore.ToString();
        if (Dead == false)
        {
            transform.position += transform.forward * MoveSpeed * Time.deltaTime;

            if(ButtonsUp == true)
            {
                steerDirection = Input.GetAxis("Horizontal");
            }
            transform.Rotate(Vector3.up * steerDirection * SteerSpeed * Time.deltaTime);

            PositionsHistory.Insert(0, transform.position);

            int index = 0;
            foreach (var body in BodyParts)
            {
                Vector3 point = PositionsHistory[Mathf.Clamp(index * Gap, 0, PositionsHistory.Count - 1)];

                Vector3 moveDirection = point - body.transform.position;
                body.transform.position += moveDirection * BodySpeed * Time.deltaTime;

                body.transform.LookAt(point);

                index++;
            }
        }
    }

    public void OnRightButtonDown()
    {
        steerDirection = 1;
        ButtonsUp = false;
    }
    public void OnLeftButtonDown()
    {
        steerDirection = -1;
        ButtonsUp = false;
    }
    public void OnButtonUp()
    {
        steerDirection = 0;
        ButtonsUp = true;
    }

    private void GrowSnake()
    {
        GameObject LastBody = gameObject;
        if(BodyParts.Count > 0)
        {
            LastBody = BodyParts.Last().gameObject;
        }
        GameObject body = Instantiate(BodyPrefab,LastBody.transform.position,LastBody.transform.rotation);
        BodyParts.Add(body);
        NewScore += 1;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<Food>())
        {
            if (other.GetComponent<Food>().Colled == false)
            {
                Eat.Play();
                GrowSnake();
                Destroy(other.gameObject);
                other.GetComponent<Food>().Colled = true;
                FoodSpawnerController.SpawnFood();
            }
        }
    }

    public void OnEndGame()
    {
        manager.GiveScoreAndCoins(); <---
    }
}
  • Вопрос задан
  • 980 просмотров
Пригласить эксперта
Ответы на вопрос 1
@GLeBaTi
Вы пытаетесь получить компонент Skins, который, якобы, висит на главной камере.
Skins skins = Camera.main.GetComponent<Skins>();
Но его нет на камере.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы