- Home /
Code Not working
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Size_Changer : MonoBehaviour
{
public Vector3 maxLocalScale;
float maxlocalScaleMagnitude;
public Text CountText;
void Start()
{
maxLocalScale = new Vector3(0.1F, 1, 0.1F);
maxlocalScaleMagnitude = maxLocalScale.magnitude;
CountText = GameObject.Find("CountTextObject"); // No work line
}
void LateUpdate()
{
float actualLocalScaleMagnitude = transform.localScale.magnitude;
if (actualLocalScaleMagnitude < maxlocalScaleMagnitude)
{
transform.localScale += new Vector3(0.0005F, 0.001F, 0.0005F);
}
else
{
Score1.score++;
print("your score is now:" + Score1.score);
Destroy(gameObject);
CountText.text = Score1.score.ToString ();
}
}
}
// Assets/Scripts/Size_Changer.cs(17,32): error CS0029: Cannot implicitly convert type //`UnityEngine.GameObject' to `UnityEngine.UI.Text'
Hi. Try to use this: CountText = GameObject.Find("CountTextObject").GetComponent();
Answer by tormentoarmagedoom · Sep 18, 2017 at 09:50 AM
Good day @nzmichael118 !
In line 10 you defined CounText as a Text component
public Text CountText;
But at line 17, you are looking for a gameobject called CountText, but Countext is not an object, is a component Text from some object
CountText = GameObject.Find("CountTextObject");
So, you should define Countextxt as text component of the object:
CountText = GameObject.Find("CountTextObject").GetComponent<Text>();
So now, yes, CountText.text can be edited with
CountText.text = Score1.score.ToString ();
If this post helps, mark as good and upvote!
If need more help, just ask using @tormentoarmagedoom
Bye :D !
Your answer
Follow this Question
Related Questions
Why can't I drag text into public text in the inspector when instantiated? 1 Answer
UI Text.color not assigning? 2 Answers
Modified Roll a Ball Tutorial,Modified Roll a Ball tutorial question 1 Answer
Display text above prefabs in Unity 4.6 0 Answers
Which type of Variable do I need to use to change the Value of a text UI object ? 0 Answers