1

While using method inside the script where it was declared it is not working and gives NullRefferenceException, but in another script it works. The method im talking about is: RestartLevel

Here is the code of first script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelInfo : MonoBehaviour {

    public GameObject endGameMenu;
    [SerializeField] GameObject holePref;
    [SerializeField] GameObject holePrefCollider;

    Ball ball;
    TurnOffGroundCollider holeUpCollider;
    Hole hole;
    Score score;
    bool menuIsInstantiated = false;

    private void Start()
    {
        ball = FindObjectOfType<Ball>();
        score = FindObjectOfType<Score>();
        InstanciateHole();
    }

    private void Update()
    {
            if (ball.transform.position.x > -4.9f &&
            ball.GetComponent<Rigidbody2D>().velocity == new Vector2(0, 0))
            {
                    Debug.Log("Condition 1 done");
                    if (GameObject.FindGameObjectsWithTag("Menu").Length < 1)
                    {
                        Debug.Log("Condition 2 done");
                        if (menuIsInstantiated == false)
                        {
                            Debug.Log("Condition 3 done");
                            GameObject menu = InstantiateMenu();
                            menu.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
                            menuIsInstantiated = true;
                        }

                    }     
            }
    }

    public void RestartLevel()
    {
        ball.DefineStartPosition();
        ball.isAbleToPush = true;
        InstanciateHole();
    }

    public GameObject InstantiateMenu()
    {
        Vector3 menuPosition = new Vector3(0, 0, 0);
        GameObject a = Instantiate(endGameMenu, menuPosition, Quaternion.identity);
        return a;
    }

    public void RestartGame()
    {
        //score.ResetScore();
        ball.DefineStartPosition();
        ball.isAbleToPush = true;
        menuIsInstantiated = false;
    }

    public void NewGame()
    {
        Destroy(GameObject.FindGameObjectWithTag("Menu"));
        menuIsInstantiated = false;
        RestartLevel();

    }

    public Vector3 DefineHolePosition()
    {
        float positionX = Random.Range(-1, 8);
        float positionY = 0;
        float positionZ = 0;
        Vector3 position = new Vector3(positionX, positionY, positionZ);
        return position;
    }
    public void InstanciateHole()
    {
        Vector3 position = DefineHolePosition();
        Instantiate(holePref, position, Quaternion.identity);
        Vector3 difference = new Vector3(0, -0.61f, 0);
        Vector3 positionCollider = position + difference;
        Instantiate(holePrefCollider, positionCollider, Quaternion.identity);
    }
}

Here is a code of another script where this method works

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hole : MonoBehaviour {

    //[SerializeField] GameObject hole;
    Score objectScore;
    LevelInfo levelInfo;
    public bool holeIsHit = false;


    private void Start()
    {
        objectScore = FindObjectOfType<Score>();
        levelInfo = FindObjectOfType<LevelInfo>();
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        holeIsHit = true;
    }
    private void Update()
    {
        if(holeIsHit == true)
        {
            objectScore.AddScore();
            Destroy(gameObject);
            levelInfo.RestartLevel();
            holeIsHit = false;
        }
    } 
}

Here is the script which variables are changed in the RestartLevel

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

    [SerializeField] float max = 10;
    [SerializeField] float current = 0;

    private Rigidbody2D rb;
    Vector2 ballVelocity = new Vector2();

    public Trajectory trajectory;
    public Vector2 ballStartPosition = new Vector2(-5, 0);

    public bool isAbleToPush = true;

    void Start ()
    {
        rb = GetComponent<Rigidbody2D>() as Rigidbody2D;
        DefineStartPosition();
    }

    void Update()
    {
        ThrowTheBall();
    }

    private void ThrowTheBall()
    {
        if (isAbleToPush)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                StartCoroutine("changeVelocity");

            }

            if (Input.GetButtonUp("Fire1") || current >= max)
            {
                StopCoroutine("changeVelocity");
                rb.velocity = ballVelocity;
                current = 0;
                isAbleToPush = false;
                ballVelocity.Set(0, 0);

            }
        }
    }

    IEnumerator changeVelocity()
    {
        while (true)
        {
            current += 0.04f;
            ballVelocity.Set(current, current);
            trajectory.ShowTrajectory(transform.position, ballVelocity);

            yield return new WaitForSeconds(0.01f);
        }
    }

    public void DefineStartPosition()
    {
        transform.position = ballStartPosition;
        rb.velocity = ballVelocity;
    }
}
Paula
  • 11
  • 1
  • Please don´t paste your entire code, just the relevant parts. – HimBromBeere Dec 08 '19 at 17:56
  • so the question was somewhat unhelpfully closed, but your issue is that you've forgotten to add a `Ball` component to your other GameObject. – Fredrik Schön Dec 08 '19 at 18:00
  • @FredrikSchön But why it works in 2 script? There is also no Ball component – Paula Dec 08 '19 at 18:26
  • @FredrikSchön im new to it, could you please specify what exactly should i change to make it work? Would be very grateful – Paula Dec 08 '19 at 18:29
  • `ball = FindObjectOfType();` this is null and needs to be fixed, I think. You can debug with Debug.Log() to see what is null. I'm sorry I can't help more than this without debugging the code and your game. – Fredrik Schön Dec 08 '19 at 19:09

0 Answers0