Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Oct 20, 2020 at 04:32 PM by I_Am_Err00r for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by I_Am_Err00r · Oct 08, 2018 at 06:32 PM · buildbuild-errorbuild settings

Build only script error

I have a project that behaves normally in the Editor mode when I play test it, but when I build it and then test the same project, I get a reference error on a script that doesn't produce this error in the editor, nor should it produce this error in the build to the best of my knowledge; I've included the code I'm using for a reference:

 public class JumperInventory : Inventory
 {

     GameObject player;

     private void Awake()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }

     protected override bool AddItemToArray(InventoryItem itemToAdd, int quantity)
     {
         player.GetComponent<CharacterJump>().NumberOfJumps++;
         player.GetComponent<CharacterJump>().NumberOfJumpsLeft++;
         Debug.Log("New Jump?");
         return base.AddItemToArray(itemToAdd, quantity);
     }
 }

What I'm trying to do is have this of course increase the number of jumps when it gets picked up, however when I run it in the build it always points to a null reference on the NumberOfJumps and NumberOfJumpsLeft only in the build, in the editor it works fine.

Would anyone know why it would behave differently in the build versus the editor, or provide any insight as to why it has trouble finding reference in the build versus the editor?

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by SkaredCreations · Oct 09, 2018 at 02:57 PM

May be that :

  • it's getting a different gameObject that also has the tag "Player" but hasn't the component CharacterJump attached

  • the player object is not found in the scene when Awake is executed

The NullPointException let us suppose that player is null or it hasn't the CharacterJump component attached.

Anyway you should optimize your code by caching CharacterJump instead of searching for it every time you call AddItemToArray:

 GameObject player;
 CharacterJump characterJump;
 
 void Awake()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     if (player != null)
         characterJump = player.GetComponent<CharacterJump>();
 }
 
 protected override bool AddItemToArray(InventoryItem itemToAdd, int quantity)
 {
     if (characterJump != null)
     {
         characterJump.NumberOfJumps++;
         characterJump.NumberOfJumpsLeft++;
     }
     Debug.Log("New Jump?");
     return base.AddItemToArray(itemToAdd, quantity);
 }
Comment
Add comment · Show 1 · 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
avatar image I_Am_Err00r · Oct 09, 2018 at 03:53 PM 0
Share

After posting this yesterday I decided to scale back and use a simple cube object and noticed this script worked on that ins$$anonymous$$d of my player prefab I had been testing with; come to find out one of the child objects of my player prefab had the tag player which of course didn't have the component to it.

It's always something so insanely stupid that trips me up the most! Thanks for the reply, it was your first answer!

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

[Unity 2017.3.0f3] Cannot open my build it Crashes (with crash Raport) 1 Answer

ios build problem 1 Answer

Not detecting touch in android device if Gradle build system with Build app bundle (Google play) 0 Answers

[Cloud build] iOS build error after changing compilation settings from Xcode 8.3 to Xcode 9+ 0 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