0

I am new to unity and im trying to make a score-counter for my game. So I made a Text object in: GameObject < UI < Text. Which I then "put" in my script.

The code is as following :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Example : MonoBehaviour {

 public Text helloText = new Text();

 void Start () {
     helloText.text = "Hello";
 }


}

I now tried to initialize the variable as one of the users said but im now getting this following error:

UnityEngine.UI.Text.Text()' is inaccessible due to its protection level

tomSurge
  • 246
  • 1
  • 3
  • 14
  • @Habib I tried doing what you said but it said the following: cannot implicitly convert type `string' to `UnityEngine.UI.Text'. Please help man. Ive been trying to fix this for a long time. The other post did not help me at all. – tomSurge Mar 03 '15 at 14:11
  • Post a new question, with exactly what have you tried and your new error. – Habib Mar 03 '15 at 14:16
  • I have to wait 90 minutes:( can you just not remove the duplicate mark? I edited the question. – tomSurge Mar 03 '15 at 14:18
  • how did you initialize the object, put that code in your question as well. – Habib Mar 03 '15 at 14:20
  • Remove the Null Reference Exception details, since that is not what you are getting anymore. otherwise someone else might close your question as duplicate. Also put initialization code in your question. – Habib Mar 03 '15 at 14:21
  • Just saw your question again, you are initializing the property which is *wrong*, simply do `public Text helloText = new Text();` – Habib Mar 03 '15 at 14:23
  • I edited the code. Habib, I did what you asked for and now getting UnityEngine.UI.Text.Text()' is inaccessible due to its protection level – tomSurge Mar 03 '15 at 14:26
  • I have reopened the question, Someone with unity experience would be able to answer your question. – Habib Mar 03 '15 at 14:28
  • @Habib doing `new Text()` is awful advice, it shouldn't even compile in Unity. `public Text helloText;` is completely fine, since it is populated by Unity engine according to values set in the editor. – Max Yankov Mar 03 '15 at 15:07
  • 1
    @golergka, I agree, the question started with `NullReferenceException` and that was my first suggestion, but later realizing that it is unity I reopened the question *(previously closed as dupe by me)* since I have no experience with Unity. – Habib Mar 03 '15 at 15:10

1 Answers1

1

Unfortunately, in Unity you can't use constructors to create Component objects, and UnityEngine.UI.Text inherits from it. Just create a public Text property, then create a new GameObject with Text component on it (easily done with GameObject > UI > Text menu) and link it to your Example behaviour in the scene.

Max Yankov
  • 10,076
  • 10
  • 54
  • 116
  • This is what I did. I made a gameobject with a text component attached to it (GameObject - UI - Text). I then attached it to my script with my public text property. I still get the error messages above. – tomSurge Mar 03 '15 at 14:48
  • That's because you still have `new Text()` in your script. You can't run `Text()`, because constructor is inaccessible. Instead, just leave it as a public property without a default value. Then, at the moment when `Start` method is called, the property will be populated by Unity. – Max Yankov Mar 03 '15 at 15:06