-1
\$\begingroup\$

I have a scene Scene001.

and a few UI objects which is Canvas and its child object Text. I have another scripted EmptyObject.

.Canvas (Tag: "canvas01")
...Text (Tag: "textTag")

.EmtpyObject (Script: "UniversalScript.cs")

I want to change the text in Text UI programmatically using the UniversalScript.cs script in EmptyObject.
However when I run it in Android devices, it keep telling me
NullReferenceException: Object reference not set to an instance of an object
I've tried the following methods and they are not working.

//01:
GameObject gmobjCanv = GameObject.FindGameObjectWithTag("canvas01");
Canvas canvas = gmobjCanv.GetComponent<Canvas>();
Text textt = canvas.GetComponentInChildren<Text>(); //  <- Error Here
textt.text = "someText";


and

//02:
GameObject gmobjTxt = GameObject.FindGameObjectWithTag("textTag");
Text textt = gmobjTxt.GetComponent<Text>();// <- Error Here
textt.text = "replace with someText";

How should I do to solve this? Thanks.

-----------------------------------
Update 001: I've pointed in my code where the error occurs.
The error is telling me that the component [Text] in my "textTag" object is not found. But this is obviously easy to find right? just change text what? Again, I just want to change my text programmatically but it can't be done in my Android device. I don't found how is my question being duplicated? They are not solving my issue but explain generally how to avoid the issue.
BTW, somehow yes, it works in PLAY MODE, I din't build for other platforms, only for Android but it not works. I suspect my code might not really correct so I need a correct way to find and change my text.

\$\endgroup\$

closed as off-topic by Gnemlock, DMGregory, Tyyppi_77, Alexandre Vaillancourt, Jimmy Jul 19 '17 at 15:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem must provide a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog. For more information, see this meta thread." – DMGregory, Jimmy
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Possible duplicate of NullReferenceException in Unity \$\endgroup\$ – Hellium Jul 8 '17 at 12:25
  • \$\begingroup\$ Are you saying this error happens only when running on Android, and not when running on desktop? If so, this might be distinct from the suggested duplicate, but we'll need a minimal, complete, verifiable example to be able to reproduce and investigate the issue. \$\endgroup\$ – DMGregory Jul 8 '17 at 22:59
  • \$\begingroup\$ I don't understand how does that question can answer my question? \$\endgroup\$ – elliotching Jul 9 '17 at 6:06
  • \$\begingroup\$ @DMGregory I din't built for desktop platform but in play mode it works \$\endgroup\$ – elliotching Jul 9 '17 at 6:10
  • \$\begingroup\$ alright I don't know what did I did. It just works now. \$\endgroup\$ – elliotching Jul 9 '17 at 9:07
1
\$\begingroup\$

Something does not exist and is null. You need to figure out what that something is.

if (!gmobjCanv) { Debug.LogError("gmobjCanv is null!"); }

if (!gmobjTxt) { Debug.LogError("gmobjTxt is null!"); }

etc.

Once you figure out what is null, perhaps it's not in the scene, or the component is not on the object you're trying to access, or the object's tag is not set - but this is up to you to solve with your code and your scene.

\$\endgroup\$
  • \$\begingroup\$ No, I just want to change my text. You are telling me a way to check if the instances is existed but I'm pretty sure they are existed. I just can't find it using my code shown above. \$\endgroup\$ – elliotching Jul 9 '17 at 6:07
  • \$\begingroup\$ I mean component <Text> \$\endgroup\$ – elliotching Jul 9 '17 at 6:08
  • \$\begingroup\$ alright I don't know what did I did. It just works now. \$\endgroup\$ – elliotching Jul 9 '17 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.