- Home /
Question by
mail2sawan · Aug 05, 2015 at 09:01 AM ·
positiontextvector3
Display text on Cloud sprites
By the following script I randomly spawning my clouds into the screen. Now I want to display some text on cloud sprite. Text will be different for every cloud and move with the cloud. Please help me out how can I do this?
using UnityEngine;
using System.Collections;
public class CloudSpawnerScript : MonoBehaviour {
[SerializeField]
private GameObject[] clouds;
public float distanceBetweenClouds = 2.0f;
public static float minX, maxX;
private Player playerScript;
public float lastCloudPositionY;
[SerializeField]
private GameObject[] cloudsInGame;
[SerializeField]
private GameObject[] collectables;
public float lastPositionX;
private int controllX;
void Awake(){
Vector2 t = Camera.main.ScreenToWorldPoint (new Vector2 (Screen.width, Screen.height));
maxX = t.x - 0.5f;
minX = -t.x + 0.5f;
float center = Screen.width / Screen.height;
CreateClouds (center);
for (int i = 0; i < collectables.Length; i++) {
collectables[i] = Instantiate(collectables[i]) as GameObject;
collectables[i].SetActive(false);
}
}
// Use this for initialization
void Start () {
playerScript = GameObject.Find ("Player").GetComponent<Player> ();
cloudsInGame = GameObject.FindGameObjectsWithTag ("Cloud");
Vector3 temp = new Vector3 (cloudsInGame [0].transform.position.x, cloudsInGame [0].transform.position.y + 0.6f, playerScript.transform.position.z);
playerScript.transform.position = temp;
}
void OnTriggerEnter2D(Collider2D target) {
if (target.tag == "Deadly" || target.tag == "Cloud") {
Vector3 temp = target.transform.position;
Shuffle (clouds);
for (int i = 0; i < clouds.Length; i++) {
if (!clouds [i].activeInHierarchy) {
temp.x = Random.Range (minX, maxX);
temp.y -= distanceBetweenClouds;
lastCloudPositionY = temp.y;
clouds [i].transform.position = temp;
clouds [i].SetActive (true);
int random =Random.Range(0, collectables.Length);
if (clouds[i].tag != "Deadly"){
//if(!collectables[random].activeInHierarchy){
// collectables[random].SetActive(true);
// collectables[random].transform.position = new Vector3 (clouds[i].transform.position.x - 0.2f,
// clouds[i].transform.position.y + 0.7f,
// clouds[i].transform.position.z);
//}
}
}
}
}
}
void CreateClouds(float positionY) {
Shuffle (clouds);
for (int i = 0; i < clouds.Length; i++) {
clouds [i] = Instantiate (clouds [i], Vector3.zero,Quaternion.identity) as GameObject;
Vector3 temp = clouds [i].transform.position;
float randomX = Random.Range (minX, maxX);
temp.x = randomX;
temp.y = positionY;
lastCloudPositionY = temp.y;
clouds [i].transform.position = temp;
positionY -= distanceBetweenClouds;
}
}
void Shuffle(GameObject[] array) {
for(int i = 0; i < array.Length; i++) {
GameObject temp = array[i];
int random = Random.Range(i, array.Length);
array[i] = array[random];
array[random] = temp;
}
}
}
Comment
Answer by Arshia001 · Aug 17, 2015 at 09:27 AM
Add a canvas text or a text mesh to the cloud objects. Also, you should generally try and watch a few video tutorials.