Je viens de créer un script qui sert à compter et à ramasser les feuilles que j'ai placer sur ma map. Ce script là je l'ai placé sur mon First Person Controller car c'est grâce à lui que je vais pouvoir les ramasser.
Cependant, dans mon Canvas j'ai un texte avec qui je voudrais qu'il affiche mon score sous la forme de "Papers: 0".
J'ai regarder de la doc et voilà mon script de base:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PaperCounter : MonoBehaviour
{
public int Paper = 0;
public int paperToWin = 6;
// Use this for initialization
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "Paper")
{
Paper += 1;
Destroy(other.gameObject);
}
}
void OnGUI()
{
if (Paper < paperToWin)
{
GUI.Box(new Rect((Screen.width / 2) - 100, 10, 200, 35), "" + Paper + " Papers");
}
else
{
GUI.Box(new Rect((Screen.width / 2) - 100, 10, 200, 35), "All Papers Collected!");
}
}
// Update is called once per frame
void Update ()
{
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PaperCounter : MonoBehaviour
{
public int Paper = 0;
public int paperToWin = 6;
Text counterPaper;
void Awake()
{
counterPaper = GetComponent<Text>();
Paper = 0;
}
// Use this for initialization
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "Paper")
{
Paper += 1;
Destroy(other.gameObject);
}
}
// Update is called once per frame
void Update ()
{
if(Paper < paperToWin)
{
counterPaper.text = "Papers: " + Paper;
}
else
{
counterPaper.text = "All papers collected";
}
}
}