Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Feedback NullReferenceException: Object reference not set to an instance of an object ScoreUI.UpdateScore (Sy

Discussion in 'Editor & General Support' started by Leoeloeleo, Apr 26, 2023.

  1. Leoeloeleo

    Leoeloeleo

    Joined:
    Mar 12, 2022
    Posts:
    1
    Screenshot 2023-04-26 at 09.58.43.jpg Screenshot 2023-04-26 at 09.59.10.jpg Screenshot 2023-04-26 at 10.01.04.jpg I get the error in the title.

    I'm very new to coding, and my issues are the following:

    -When the player collides with a "Food" It does not disappear after eating it, but the player still gets bigger
    -The Score on the screen does not increase when eating foods

    PlayerController:

    using UnityEngine;
    public class PlayerController : MonoBehaviour
    {
    public float moveSpeed = 5.0f;
    public float maxSize = 5.0f;
    public int maxScore = 100;
    public float playerScaleFactor = 0.1f;
    private Vector2 movement;
    private float currentSize = 0.7f;
    private int currentScore = 0;
    private Vector2 targetPosition;
    public ScoreUI scoreUI;
    private void Start()
    {
    scoreUI = GameObject.Find("Canvas").GetComponentInChildren<ScoreUI>();
    targetPosition = transform.position;
    }
    private void Update()
    {
    HandleInput();
    targetPosition += movement * moveSpeed * Time.deltaTime;
    transform.position = Vector2.Lerp(transform.position, targetPosition, 0.1f);
    }
    private void HandleInput()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    movement = new Vector2(moveHorizontal, moveVertical).normalized;
    if (movement.magnitude > 0)
    {
    float angle = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg + 270f;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    SpriteRenderer spriteRenderer = GetComponentInChildren<SpriteRenderer>();
    if (spriteRenderer != null)
    {
    float spriteAngle = angle;
    if (spriteRenderer.transform.localScale.x < 0)
    {
    spriteAngle += 180;
    }
    spriteRenderer.transform.rotation = Quaternion.AngleAxis(spriteAngle, Vector3.forward);
    }
    }
    }
    public void IncreaseSize(float sizeIncrease)
    {
    currentSize += sizeIncrease;
    if (currentSize > maxSize)
    {
    currentSize = maxSize;
    }
    transform.localScale = new Vector3(currentSize, currentSize, currentSize) * playerScaleFactor;
    }
    public void UpdateScore(int scoreIncrease)
    {
    currentScore += scoreIncrease;
    if (currentScore > maxScore)
    {
    currentScore = maxScore;
    }
    scoreUI.UpdateScore(currentScore);
    }
    }


    ScoreUI:

    using UnityEngine;
    using UnityEngine.UI;
    public class ScoreUI : MonoBehaviour
    {
    public Text scoreText;
    private void Start()
    {
    UpdateScore(0);
    }
    public void UpdateScore(int score)
    {
    scoreText.text = "Score: " + score;
    }
    }

    FoodController:


    using UnityEngine;
    public class FoodController : MonoBehaviour
    {
    public int scoreValue = 10;
    public int minPoints = 1;
    public int maxPoints = 1;
    public float minSize = 0.1f;
    public float maxSize = 0.3f;
    public float moveSpeed = 2.0f;
    private int pointValue;
    private float foodSize;
    private Rigidbody2D rb2d;
    private void Start()
    {
    bool hasRigidbody2D = gameObject.GetComponent<Rigidbody2D>() != null;
    // Assign a random size and point value to the food object
    pointValue = Random.Range(minPoints, maxPoints + 1);
    foodSize = Random.Range(minSize, maxSize);
    transform.localScale = new Vector3(foodSize, foodSize, foodSize);
    // Get a reference to the rigidbody component
    rb2d = GetComponent<Rigidbody2D>();
    // Apply a random initial velocity to the food object
    Vector2 randomVelocity = Random.insideUnitCircle.normalized * moveSpeed;
    rb2d.velocity = randomVelocity;
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    PlayerController playerController = other.GetComponent<PlayerController>();
    if (playerController != null)
    {
    playerController.IncreaseSize(foodSize);
    playerController.UpdateScore(pointValue);
    }
    else
    {
    Debug.LogError("PlayerController component not found on Player object!");
    }
    Destroy(gameObject); // destroy this game object
    }
    }
    }
    Screenshot 2023-04-26 at 09.58.43.jpg Screenshot 2023-04-26 at 09.59.10.jpg Screenshot 2023-04-26 at 10.01.04.jpg



    I want the rocket to eat the stars, adding to the ui score and make the stars disappear after colliding.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656