- Home /
I made a text object in a script but Unity doesnt let me place the UI text in the box.
The Text object doesnt get placed in the Txt box in Destroy By Contact 21 Script.
NOTE: I am using Unity 5
Here is the C# script:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class DestroyByContact21 : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public Text txt;
private int currentscore=0;
void Start () {
txt = gameObject.GetComponent<Text>();
txt.text="Score : " + currentscore;
}
void Update () {
txt.text="Score : " + currentscore;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Boundary"))
{
return;
};
Instantiate (explosion, transform.position, transform.rotation);
if (other.gameObject.CompareTag ("Player"))
{
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
};
Destroy (other.gameObject);
Destroy (gameObject);
}
}
im not sure what youre trying to accomplish could you plz explain what is happending and what you want it to do ins$$anonymous$$d
I actually realised that you cannot drag in objects from the hierarchy into the prefab's variable fields. Is there any way to delete the question? I have already found the answer.
$$anonymous$$ake that youre answer ins$$anonymous$$d of deleteting the post
Answer by malkere · Jul 03, 2015 at 03:11 PM
if it's a prefab it doesn't yet exist. if you've dragged it to the scene and it does exist, you can't drag a UI text into a Text type because Text is a subcomponant of the UIText, called with UIText.Text. you need to define a GameObject or a Transform to fill it with a UI object.
Your answer
Follow this Question
Related Questions
Modifying line order in UI Text component 0 Answers
How do I use custom input validation on an InputField? 1 Answer
Get Position of a specific letter in UI Text containing line breaks 0 Answers
If i have a script how to I input an int from there to a text UI? 1 Answer
UI Text 2 Color Horizontal Fill With Sliding Dividing Line 0 Answers