Question by
Gemspark · May 26, 2016 at 05:20 PM ·
scripting problemscript error
What's wrong with my script?
using UnityEngine;
using System.Collections;
public class ball_controller : MonoBehaviour {
public float speed;
public Text amountText;
private Rigidbody rb;
private int amount;
void Start ()
{
rb = GetComponent<Rigidbody>();
amount = 0;
SetAmountText ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Dot"))
{
other.gameObject.SetActive (false);
amount = amount + 1;
SetAmountText ();
}
}
void SetAmountText ()
{
amountText.text = "Amount: " + amount.ToString ();
}
}
I get the error:
MissingReferenceException: The object of type 'Object' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
and
Assets/_Scripts/ball_controller.cs(7,16): error CS0246: The type or namespace name `Text' could not be found. Are you missing a using directive or an assembly reference?
Comment
Answer by Jessespike · May 26, 2016 at 07:17 PM
Add using UnityEngine.UI;
to the top of your script. Make sure amountText has a reference in the Inspector.
Ah, I forgot to reference amountText. Being tired does things to you, haha.
Thanks for the help! Adding "using UnityEngine.UI;" fixed the issue with the text.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081636im_/https://answers.unity.com/themes/thub/images/avi.jpg)