• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sarseth · Apr 20, 2015 at 06:28 AM · javascriptunity5script errorexecutablescript execution order

GameObject.Find() on build returns NRE. In editor works fine.

Hi there guys.

Have a problem, don't know where to look. Order in compilation or what. Here is the hierarchy:

CanvasDialogue (Canvas - prefab)
-- DialogueBox (Text)

NPC (prefab of npc)
-- DialogObject (which contains script DialogBoxScript.cs)

DialogBoxScript.cs location: Assets/Scripts/UI

 public class DialogBoxScript : MonoBehaviour {
 void Start () {
     owner = GetComponentInParent<NpcCharacter> ();
     if (owner == null) {
         Debug.LogError ("owner is null");
     }
     hideTextBox ();
     player = GameObject.Find ("Player").GetComponent<Player> ();
     text = GameObject.Find ("CanvasDialogue").GetComponentInChildren<Text> ();
     if (text == null) {
         Debug.LogError ("text is null");
     }
 }


NRE occurs on line 9. Well in build only, when I run exe. When I run game in unity, everything works as it should. Where to look? Cause honestly, have no idea.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Apr 20, 2015 at 06:26 AM 1
Share

What is an NPE? Do you mean NRE? (null reference exception?)

Always format code with the 101010 button.

Use a double-space at the end of a line prior to hitting return to create a newline character. Yes, it's weird; no, we don't like it either. ;)

Type something...(double space + return)
Something on a new line.

I assume you failed to do so, making your hierarchy description impossible to decipher. I edited your question to resolve these issues.

The fact that this manifests only in builds is a little mysterious, so I suppose this should pass moderation.

avatar image Baste · Apr 20, 2015 at 07:16 AM 1
Share

Line 9 giving a null reference means that GameObject.Find("CanvasDialogue") is returning null.

Is that object by any chance spawned? It might be that the order of Start calls somehow changes between editor time and build time, and that makes whatever spawns the CanvasDialogue object not be spawned yet.

If that's the case, move the spawning code to an Awake call.

avatar image Sarseth · Apr 21, 2015 at 04:38 AM 0
Share

@AlwaysSunny: Thank u, yes I meant NRE (i'm daily java developer).

@Baste: unfortunatly no. Object is not spawned by code. It is there injected in unity in hierarchy.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sarseth · Apr 21, 2015 at 05:08 PM

Problem is gone, but have no f idea what was it. Made workaround. Removed CanvasDialogue (Canvas - prefab) -- DialogueBox (Text)

And moved DialogueBox to other Canvas which was visible in build exe.

I removed and recreated second canvas and still got a problem while on build, but not in editor.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 5 Mouse Look Problem 1 Answer

CS1025: Single-line comment or end-of-line expected,error CS1025: Single-line comment or end-of-line expected 0 Answers

'other' is not a member of 'Object'? 1 Answer

_Data folder not found 2 Answers

MeshCombineUtility problem after upgrade to Unity 5? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges