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: 1
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: