Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Score count not changing

Discussion in 'Scripting' started by Selphie12, May 2, 2015.

  1. Selphie12

    Selphie12

    Joined:
    May 2, 2015
    Posts:
    2
  2. Selphie12

    Selphie12

    Joined:
    May 2, 2015
    Posts:
    2
    I also found i'm getting this error code now can someone explain this?

    NullReferenceException: Object reference not set to an instance of an object
    Pickups.SetCountText () (at Assets/Game Scripts/Pickups.cs:40)
    Pickups.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Game Scripts/Pickups.cs:33)
     
  3. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    Well to answer your error, a NullReferenceException is an error when you're trying to use something that's either been set to null by yourself, or it just doesn't contain anything.

    What is that Text variable, is it another script?
     
  4. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    769
    Here it worked perfectly

    This error is because you did not drag the text to the field of property in the inspector

    You must be forgetting to do something in the process

    Create a new scene for testing and follow these steps :

    - Create a cube in the scene
    - Attach the script to this cube
    - Drag the text object into the field of property in the inspector
    - Add to the cube the rigidbody component
    - Disable the "Use Gravity" option in rigidbody component

    - Create a sphere
    - Activate the "Is Trigger" on the sphere collider
    - Create the tag "Pick Up"
    - Add this tag to the sphere

    - Press play and control the cube to collide with the ball, if you did everything right it should work

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Pickups : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public Text countText;
    9.    
    10.     private int count;
    11.    
    12.    
    13.     void Start ()
    14.     {
    15.         count = 0;
    16.         SetCountText ();
    17.     }
    18.    
    19.     void FixedUpdate ()
    20.     {
    21.         float moveHorizontal = Input.GetAxis ("Horizontal");
    22.         float moveVertical = Input.GetAxis ("Vertical");
    23.        
    24.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    25.  
    26.         transform.Translate((movement*10)*Time.deltaTime);
    27.        
    28.     }
    29.    
    30.     void OnTriggerEnter(Collider other)
    31.     {
    32.  
    33.         if (other.gameObject.CompareTag("Pick Up"))
    34.         {
    35.             Debug.Log("collided");
    36.             count +=1;
    37.             SetCountText ();
    38.             Destroy(other.gameObject);
    39.         }
    40.     }
    41.    
    42.     void SetCountText ()
    43.     {
    44.         countText.text = "Count: " + count.ToString();
    45.     }
    46. }

    Denis Lemos