Moving UI Text

@OboShape I have merged your code with mine to get:

But here is all I get in the console:

Even though Canvas2 is defined and in the scene:

Is Canvas2 a prefab? or dragged into the slot from the scene?

Dont have Unity for another hour or so, just rebuilding PC :frowning:

I tried both, for the above I dragged it in from the Hierarchy.

I don’t think that you need to do all that coding just to instantiate it, mine is working just fine and it is as simple as possible, you don’t have to create a empty gameobject and add a canvas as child to it, would be better to have the canvas as prefab and the text as child, with the canvas render mode set to world space and fine tuning the scale to fit your need. I think that you can have something as simple as:

text with the following script:

using UnityEngine.UI;

public class Points : MonoBehaviour {
	public Text Score;
	public int enScore;
	void Start () 
	{
		Score = GetComponent<Text>();
		Destroy(gameObject.transform.parent.gameObject,0.5f);
		Destroy(gameObject,0.6f);


	}
	void Update()
	{
 		Score.text = enScore.ToString();
		gameObject.transform.Translate(0, 1 * Time.deltaTime, 0,Space.World);

	}

}

and the Projectile with the following script added to the OnTriggerEnter2d method:

GameObject ScoreText = Instantiate (Points, gameObject.transform.position, Quaternion.identity) as GameObject;
ScoreText.transform.GetChild(0).GetComponent<Points>().enScore = this.DisplayPoints;

and then you would need to initialize the int DisplayPoints on the same script.

After that you should link the Canvas prefab (from the assets windows) to the Projectile inspector. this should work

@Joao_Dalvi: Where does that first script go if not in projectile?
Does it go on the Text itself.

The Translate function got clipped.

1 Like

It goes in the text

Ill send you a project with only this structure later today

Hey Retro, follow the link to the zipped project:
https://mega.nz/#!tVgzRTbT!M6mOjCmixfin3Eg0nnohNR-0R6e5OuyT80qKHlupvtc

Just open it as a new project, It has enough just to make it work, you should edit it to get what you need.

@Joao_Dalvi: Thanks for the simple solution.

How ever I am having trouble adapting it for the Paddle/Enemy to store its own point value
instead of in the ball/missile.

I have complete about half-a-dozen unity2D courses.
Where can I get a better grasp of the language/API so I can make lines of code like this myself?
ScoreText.transform.GetChild(0).GetComponent().enScore = this.DisplayPoints;

1 Like

You can address it directly to the enemy script when it collides, change the this.DisplayPoints for col.GetComponent<"Name of the Script">().DisplayPoints;, set a value to public int DisplayPoints on the enemy’s script and it should work.

like this:

void OnTriggerEnter2d (Collider2D col)
{

GameObject ScoreText = Instantiate (Points, gameObject.transform.position, Quaternion.identity) as GameObject;
ScoreText.transform.GetChild(0).GetComponent<PointsScript>().enScore = col.GetComponent<"Name of the Script">().DisplayPoints;


}

Regarding this:

I just google for it or I look for an answer in the unity docs, I don’t have too much experience with neither programming or unity to be honest. I wanted to set a reference to the child of that canvas and I searched on google using those keywords: “reference of child unity”. After browsing a little I found some method options.

Did you manage to solve it Retro?

Yes I finally have it working, thank you all very much.

1 Like

Awesome!! I’m glad that you made it work!!

Privacy & Terms