Search Unity

Question Call a script from a prefab to update the score

Discussion in 'Scripting' started by maia_unity29, Dec 6, 2022.

  1. maia_unity29

    maia_unity29

    Joined:
    Aug 30, 2022
    Posts:
    1
    Hi everyone,
    I'm very new to unity (just about to finish the junior dev. learning) and I'm struggling with one task.

    So basically, I have a box, when my targets collide with it, it counts points (from -3 to 1). Targets are 3 prefabs and they spawn via a spawn manager.

    I've set point value in my Target script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using static UnityEditor.PlayerSettings;
    5. using TMPro;
    6. using UnityEngine.UI;
    7.  
    8.  
    9. public class Target : MonoBehaviour
    10. {
    11.     public Rigidbody targetRb;
    12.     public float AngularSpeed;
    13.     public float Speed;
    14.  
    15.     private float poS = 0.2f;
    16.  
    17.     public AudioSource collectSound;
    18.  
    19.  
    20.     //score
    21.     public int pointValue;
    22.  
    23.  
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         targetRb = GetComponent<Rigidbody>();
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.  
    35.     }
    36.  
    37.     // Add Force to targets
    38.     private void FixedUpdate()
    39.     {
    40.         Speed = targetRb.velocity.magnitude;
    41.         AngularSpeed = targetRb.angularVelocity.magnitude;
    42.  
    43.         if(transform.position.x > poS)
    44.         {
    45.             targetRb.AddForce(Vector3.left,ForceMode.Impulse);
    46.  
    47.         }
    48.  
    49.         if(transform.position.x < poS)
    50.         {
    51.             targetRb.AddForce(Vector3.right,ForceMode.Impulse);
    52.  
    53.         }
    54.     }
    55.  
    56.     // Destroy the objects
    57.     private void OnMouseDown()
    58.     {
    59.         Destroy(gameObject);
    60.  
    61.     }
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68. }
    69.  
    I attached a script to my box a Counter in order to count points and call the Target script and its point value. But it doesn't work

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6.  
    7. public class CounterScore : MonoBehaviour
    8.  
    9. {
    10.     private int score;
    11.     public TextMeshProUGUI scoreText;
    12.  
    13.     //link to target script
    14.     public GameObject prefab;
    15.     public Target targetScript;
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         score = 0;
    22.         targetScript = GameObject.Find("Target2").GetComponent<Target>();
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.     }
    31.  
    32.  
    33.     //Update score
    34.     private void OnCollisionEnter(Collision collision)
    35.     {
    36.         UpdateScore(targetScript.pointValue);
    37.     }
    38.      
    39.  
    40.  
    41.     private void UpdateScore(int scoreToAdd)
    42.     {
    43.         score += scoreToAdd;
    44.  
    45.         scoreText.text = "Points:" + score;
    46.     }
    47.  
    48.  
    49.  
    50.  
    51.  
    52. }
    53.  
    The error is "NullReferenceException: Object reference not set to an instance of an object
    CounterScore.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Script/CounterScore.cs:34)".

    What should I do?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Most common problem reported here

    You should learn what a reference type vs a value type is in C#.

    As it says, the exception happens on Line 34. Look at line 34. "targetScript" is a reference to a Target object. You initialise that in Line 22 so obviously that line is failing to find the "Target" component. When you ask for a component, if it doesn't find it, it'll return NULL. You cannot perform work on NULL.
     
    maia_unity29 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    maia_unity29 likes this.