- Home /
Cannot drag-and-drop ui.text in inspector
Hello, iam morbidly new to unity and i have a problem.
I created a script in which iam holding a public reference to a Text variable.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class enemyScript : MonoBehaviour {
public Text scoreText;
//...
}
I also made a Text from UI > Text in the inspector. But when iam trying to assign to the script - which is part of a prefab - i cannot drop the text element into the script's variable. Here's a picture with obligatory giant red arrows: image
Answer by Mouton · Jun 06, 2015 at 02:23 PM
In order to reference an Object to a prefab, you must be sure the Object is part of the prefab, which in your case is not.
You must create an instance of your "Enemy" prefab, then drag&drop it to your "Score Text" reference.
However, since the references arn't within the parent Prefab, it won't be saved when on you "Apply".
You have to consider your problem another way. What are you trying to achieve ?
[Edit:] Mainly, when you have to deal with recurring informations potentially accessible by many objects (just like UI score), it is a good idea to have a place where you can access to all this informations easily, such as a toolbox in your garage.
You can add a script somewhere in your scene, to implement a "ScoreManager" class with datas you want to store and handy functions to update the UI.
Look at this tutorial, it is exactly about what you are trying to achieve: https://unity3d.com/learn/tutorials/projects/survival-shooter/scoring-points
Just want to add score to the player and showing it on the ui when an enemy is hit and dies. Thought it would make sence to fiddle with it in the enemy script since the collision detection takes place there.
For those who don't want to watch the whole video: Use a static variable to access things globally.
@$$anonymous$$outon The best solution that i can get to managing the score.Thanks once again
Your answer
Follow this Question
Related Questions
How can I return a Dragged Object From a Layer Group to its Original Position? 0 Answers
Transform to RectTransform Conversion 1 Answer
Using Canvas/Gui with splitscreen(several cameras) 0 Answers
UI Sliders not interactable 3 Answers
How to make a UI Canvas not appear again after the first time the user hits the "I Agree" button. 2 Answers