- Home /
Question by
mradebe_eti · Oct 31, 2013 at 09:25 AM ·
c#textvector3positioning
Help change Score text positioning using vector3 [C#]
Hi,
I need help with my script, so it reads the changes to the text position when the specified levels are loaded. Right now it does not apply any changes at all.
Here is my script:
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public static int score = 0;
void Start () {
guiText.text = " " + score.ToString();
guiText.material.color = Color.black;
guiText.alignment = TextAlignment.Left;
guiText.anchor = TextAnchor.UpperRight;
guiText.transform.position = new Vector3(0.85f,0.1f,5.181322f);
}
void OnLevelWasLoaded(int level) {
if (level == 8 || level == 503 || level == 504 || level == 505) {
GameObject.Find ("Score").guiText.text = " " + score.ToString();
guiText.material.color = Color.black;
guiText.alignment = TextAlignment.Left;
guiText.anchor = TextAnchor.UpperRight;
guiText.transform.position = new Vector3(1.85f,1.1f,5.181322f);
}
}
}
Any assistance is greatly appreciated.
Thanks.
Comment
Answer by Fornoreason1000 · Oct 31, 2013 at 10:34 AM
OnLevelWasLoaded can be called before even Awake thus before start which resets your script again. your scripts seems rather confusing...
would this be a better alternative?
public class Score : MonoBehaviour {
public static int score = 0;
void Start () {
DontDestroyOnLoad(this); //makes the object pernament....
//notably the same code used when you load the level
guiText.text = " " + score.ToString();
guiText.material.color = Color.black;
guiText.alignment = TextAlignment.Left;
guiText.anchor = TextAnchor.UpperRight;
guiText.transform.position = new Vector3(0.85f,0.1f,5.181322f);
}
void OnLevelWasLoaded(int level) {
//disables object if level is not 8, 503, 504 or 505, renables if it is.
if (level == 8 || level == 503 || level == 504 || level == 505)
{
this.enabled = true;
guiText.transform.position = new Vector3(1.85f, 0.1f, 5.181322f);
}
else
{
guiText.transform.position = new Vector3(0.85f,0.1f,5.181322f);
this.enabled = false;
}
}
}