469,300 Members | 2,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

Unity2D, can't print a public field on the screen

I am doing a 2D platformer using Unity, there is an object that moves by a script (the character). There are diamonds that are put on the level scene, they can be collected by the character. The character's class has a public field that saves the amount of collected diamonds . It's starting value is zero. I managed to make it so that when touching a diamond, this variable increases by 1, and the diamond is destroyed. But I can't display the value of this field on the screen. I tried it like this: I created a canvas, put the text on it, hung the following script on the text:
Expand|Select|Wrap|Line Numbers
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class textScr : MonoBehaviour { 
  7.     private MovingObj _movingObj;
  8.     private Text _diamondText;
  9.  
  10.     void Start()
  11.     {
  12.         _movingObj = GameObject.Find("Square").GetComponent<MovingObj>();
  13.     }
  14.  
  15.     public void Update()
  16.     {
  17.         _diamondText.text = "Diamonds: " + _movingObj.diamondsQuantity.ToString();
  18.     }
  19.  
  20. }
  21.  
The charachte Throws "NullReferenceException: Object reference not set to an instance of an object" on line 17... Maybe, the problem is with the charcter script? It is here:
Expand|Select|Wrap|Line Numbers
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MovingObj : MonoBehaviour
  6. {
  7.     [SerializeField] private float speed = 2f; // скорость движения
  8.     [SerializeField] private int lives = 5; // скорость движения
  9.     [SerializeField] private float jumpForce = 15f; // сила прыжка
  10.     public int diamondsQuantity = 0;
  11.     private bool isGrounded = false;
  12.  
  13.     private ContactFilter2D gh;
  14.     private Rigidbody2D rb;
  15.     private SpriteRenderer sprite;
  16.  
  17.     private void Awake()
  18.     {
  19.         rb = GetComponent<Rigidbody2D>();
  20.         sprite = GetComponentInChildren<SpriteRenderer>();
  21.     }
  22.  
  23.     private void OnCollisionEnter2D(Collision2D c)
  24.     {
  25.         collisionWithDiamond(c);
  26.     }
  27.  
  28.     private void FixedUpdate()
  29.     {
  30.         CheckGround();
  31.     }
  32.  
  33.     private void Update()
  34.     {
  35.         if (Input.GetButton("Horizontal"))
  36.             Run();
  37.         if (isGrounded && Input.GetButtonDown("Jump"))
  38.             Jump();
  39.     }
  40.  
  41.     private void Run()
  42.     {
  43.         Vector3 dir = transform.right * Input.GetAxis("Horizontal");
  44.  
  45.         transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
  46.  
  47.         sprite.flipX = dir.x < 0.0f;
  48.     }
  49.  
  50.     private void Jump()
  51.     {
  52.         rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
  53.     }
  54.  
  55.     private void collisionWithDiamond(Collision2D d)
  56.     {
  57.         if (d.gameObject.tag == "Diamond") //у всех алмазов тег Diamond, сюда можно записать и тот публичный метод
  58.         {
  59.             Destroy(d.gameObject);
  60.             diamondsQuantity++;
  61.         }
  62.     }
  63.  
  64.     private void CheckGround()
  65.     {
  66.         Collider2D[] collider = Physics2D.OverlapPointAll(new Vector2 (transform.position.x, transform.position.y - 0.5f));
  67.         isGrounded = collider.Length >= 1;
  68.     }
  69. }
  70.  
How to fix it?
and yes, I know that this will update the text every second, after fixing I am going to write a public method.
2 Weeks Ago #1
0 4555

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Stan Brown | last post: by
4 posts views Thread by Ken VdB | last post: by
2 posts views Thread by gwainguard | last post: by
17 posts views Thread by asit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.